I’m thinking of switching over from DrJava to sublime text but the problem is that I don’t know how to compile and run code in sublime text. How would I go about compiling and running my code in sublime text?
Compiling and Running Java
By default, Sublime Text can compile your Java code when you press Tools > Build. However, to build and compile Java, you’ll need to create a new build system. This post should help depending on what OS you are using: https://forum.sublimetext.com/t/how-to-run-java-code-after-compiling/1295/1
You should also check out my Display Functions (Java) plugin
Available here: https://github.com/BoundInCode/Display-Functions
or through Package Control
So i’ve put the bat file in C:\Program Files\Java\jdk*\bin\
@ECHO OFF
cd %~dp1
javac %~nx1
java %~n1
and named it javacexec.bat
then I edited JavaC.sublime-build so that it says
{
"cmd": "javacexec.bat", "$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java"
}
but when press F7 or build i just get console at the bottom of the window that says
Error 2] The system cannot find the file specified
[Finished]
I’ve tried putting the file I was trying to compile and run in the root of my hard drive and running sublime text as administrator but nothing seems to work. I’m running Win7 btw.
I was having the same error trying to compile my code with ‘Ctrl+b’, which I though was odd since I have been able to compile on OSX without any issues and then run the created class with terminal.
Turns out that my environment variables we not set when installing Java. (forgive me if you have already done this)
Steps I took to remedy this
- Click Start
- Right click on ‘Computer’
- On the left hand side select ‘Advanced System Settings’
- Near the bottom click on ‘Environment Variables’
- Scroll down on ‘System Variables’ until you find ‘PATH’ - click edit with this selected.
- Add the path to your Java bin folder. Mine ends up looking like this
;C:\Program Files\Java\jdk1.7.0_03\bin\
Not sure if this is a trivial thing or not, no doubt you are meant to do this when you install the SDK but I didn’t read the instructions as per usual
As for running, I just use the CMD line at the moment.
Hope it helps!
Hey thanks for this!
I want to share with you the following .bat source code because is working like a charm!
When you press Ctrl + B sublime text will compile the .java and then will run it automatically by opening a new CMD window.
@ECHO OFF
cd %~dp1
ECHO Compiling %~nx1...
IF EXIST %~n1.class (
DEL %~n1.class
)
javac -Xlint:unchecked %~nx1
IF EXIST %~n1.class (
ECHO Running %~n1...
start cmd /k java -ea %~n1
)
Hi. If you want easier way to compile and create jars for Java projects, try my Package:
https://github.com/psychowico/SublimeJavaCompiler.
It’s working with ST2 and ST3. I’ll be glad if I get any feedback from you - and help with create better README file.
[quote=“psychowico”]Hi. If you want easier way to compile and create jars for Java projects, try my Package:
https://github.com/psychowico/SublimeJavaCompiler.
It’s working with ST2 and ST3. I’ll be glad if I get any feedback from you - and help with create better README file.[/quote]
this is fantastic! Also like the fact that you also mentioned how to configure the keyboard short cuts.
Thanks
This http://web.archive.org/web/20130509004854/http://www.compilr.org/compile-and-run-java-programs/solution worked for me.