Home Download Buy Blog Forum Support

Compile and run java in SublimeText

Compile and run java in SublimeText

Postby Slus on Fri Oct 23, 2009 8:00 pm

Hi all!

well i,ve just discovered this wonderful software... but I have a problem... is there a way to actually run a java program from sublime? i know i can compile it but is there a way or plugin that I can add to be able to run it?


Thank you very much for replying.
Slus
 
Posts: 9
Joined: Fri Oct 23, 2009 7:58 pm

Re: Compile and run java in SublimeText

Postby jps on Sat Oct 24, 2009 12:52 pm

Your best bet for running a program from within sublime is to write a simple plugin to launch your application, perhaps by using the python subprocess module.

It's possible to use the exec command to do this without using a plugin, but it's pretty tedious to get all the escaping done correctly, it's simpler to just write a plugin.
jps
Site Admin
 
Posts: 3067
Joined: Wed Mar 19, 2008 12:33 pm

Re: Compile and run java in SublimeText

Postby Slus on Sat Oct 24, 2009 2:53 pm

mhmm ok well the problem is that i dont know nothing about python.... so i would need some help... can somebody show me something simple and very detailed on how to do it!? ive seen the plugin devellopment page but it doesnt help
Slus
 
Posts: 9
Joined: Fri Oct 23, 2009 7:58 pm

Re: Compile and run java in SublimeText

Postby Slus on Tue Oct 27, 2009 12:15 am

help pl0x?
Slus
 
Posts: 9
Joined: Fri Oct 23, 2009 7:58 pm

Re: Compile and run java in SublimeText

Postby jps on Tue Oct 27, 2009 9:27 am

You'll want a plugin along these lines:

Code: Select all
import sublime, sublimeplugin
import subprocess

class RunApplicationCommand(sublimeplugin.ApplicationCommand):
   def run(self, args):
      subprocess.Popen(["C:\\path\\to\\myapp.exe", "argument1"])


This will run "C:\path\to\myapp.exe argument1"

To bind this to a key, use the command name "runApplication", eg:

Code: Select all
<binding key="f5" command="runApplication"/>
jps
Site Admin
 
Posts: 3067
Joined: Wed Mar 19, 2008 12:33 pm

Re: Compile and run java in SublimeText

Postby Slus on Tue Oct 27, 2009 5:57 pm

Ok so I tried it... and since Im not good at python I dont know what to change in order to make it work. I dont know what to replace argument1 for

This is my run.py file
Code: Select all
import sublime, sublimeplugin
import subprocess

# This simple plugin will add 'Hello, World!' to the end of the buffer when run.
# To run it, save it within the User/ directory, then open the console (Ctrl+~),
# and type: view.runCommand('sample')
#
# See http://www.sublimetext.com/docs/plugin-basics for more information
class RunApplicationCommand(sublimeplugin.ApplicationCommand):
   def run(self, args):
      subprocess.Popen(["C:\Program Files\Java\jdk1.6.0_16\bin\java.exe", "$File"])



I dont know what to do from here

In order to run a java file you need to compile it (which works)
and then run in cmd the run command

Ex:
Code: Select all
javac test.java
java test (without the .java extension)


So can anybody help?
Slus
 
Posts: 9
Joined: Fri Oct 23, 2009 7:58 pm

Re: Compile and run java in SublimeText

Postby Slus on Wed Oct 28, 2009 11:19 pm

anybody?
Slus
 
Posts: 9
Joined: Fri Oct 23, 2009 7:58 pm

Re: Compile and run java in SublimeText

Postby vim on Thu Oct 29, 2009 5:31 am

try adding
Code: Select all
print str(args)


the args you get are from extra words you add to your key binding.

'somehow' you should have the file name to compile, it could be from the args, it could be by calling view's API (see http://www.sublimetext.com/docs/api-reference) and getting the current opened file. this is up to you, and how you want the plugin to work.
vim
 
Posts: 298
Joined: Tue May 27, 2008 6:51 pm

Re: Compile and run java in SublimeText

Postby redandwhite on Fri Oct 30, 2009 12:03 am

Hi, hope this isn't too late. I stumbled into this problem myself just now, and the 10-second solution is to go to:

Preferences > Default Key Bindings

and enter the following line:

Code: Select all
<binding key="f5" command="exec '' 'java $BaseName'"/>


This will run the program when you press F5. Be sure to compile first!
redandwhite
 
Posts: 1
Joined: Thu Oct 29, 2009 11:59 pm

Re: Compile and run java in SublimeText

Postby Slus on Sat Oct 31, 2009 3:09 pm

oh my god... redandwhite... thank you very much!!!! its freaking aweosme! like seriously thank you
Slus
 
Posts: 9
Joined: Fri Oct 23, 2009 7:58 pm

Next

Return to Technical Support

Who is online

Users browsing this forum: nonolly and 9 guests