## Compile and run java in SublimeText

### Compile and run java in SublimeText

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

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

Posts: 3067
Joined: Wed Mar 19, 2008 12:33 pm

### Re: Compile and run java in SublimeText

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

help pl0x?
Slus

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

### Re: Compile and run java in SublimeText

You'll want a plugin along these lines:

Code: Select all
import sublime, sublimepluginimport subprocessclass 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

Posts: 3067
Joined: Wed Mar 19, 2008 12:33 pm

### Re: Compile and run java in SublimeText

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, sublimepluginimport 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 informationclass 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.javajava 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 anybody? Slus Posts: 9 Joined: Fri Oct 23, 2009 7:58 pm ### Re: Compile and run java in SublimeText 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 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

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