Sublime Forum

Compile and run java in SublimeText

#1

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.

0 Likes

Questions about Build Systems in Tools Menu
#2

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.

0 Likes

#3

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

0 Likes

#4

help pl0x?

0 Likes

#5

You’ll want a plugin along these lines:

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:

<binding key="f5" command="runApplication"/>
0 Likes

#6

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]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”])[/code]

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:

javac test.java java test (without the .java extension)

So can anybody help?

0 Likes

#7

anybody?

0 Likes

#8

try adding

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 sublimetext.com/docs/api-reference) and getting the current opened file. this is up to you, and how you want the plugin to work.

0 Likes

#9

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:

<binding key="f5" command="exec '' 'java $BaseName'"/>

This will run the program when you press F5. Be sure to compile first!

0 Likes

#10

oh my god… redandwhite… thank you very much!!! its freaking aweosme! like seriously thank you

0 Likes

#11

mhmm i have a new problem… I can now run the program but lets say I ask somebody to input somehting… it doesnt work I cant write anything… anyway around this?

0 Likes

#12

nobody?

0 Likes

#13

you may want to give more details about your problem.

0 Likes

#14

yes actually thank you sublimator, that is what im talking about I will try it out later when I have the time… thnx

0 Likes