Sublime Forum

Compiling and Running Java

#1

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?

0 Likes

Sublime won't display program output
'Multi-Step' Build Scripts
#2

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 :smile:
Available here: https://github.com/BoundInCode/Display-Functions
or through Package Control

0 Likes

#3

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.

0 Likes

#4

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

  1. Click Start
  2. Right click on ‘Computer’
  3. On the left hand side select ‘Advanced System Settings’
  4. Near the bottom click on ‘Environment Variables’
  5. Scroll down on ‘System Variables’ until you find ‘PATH’ - click edit with this selected.
  6. 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 :unamused:
As for running, I just use the CMD line at the moment.
Hope it helps!

0 Likes

#5

This worked for me, coupled with the exec bat method. Thanks!!!

0 Likes

#6

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 )

0 Likes

#7

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.

0 Likes

#8

[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

0 Likes

#9

This http://web.archive.org/web/20130509004854/http://www.compilr.org/compile-and-run-java-programs/solution worked for me.

0 Likes

#10

excellent, thanks

0 Likes

#11

+1 for the last posted link - should be updated for ST3

0 Likes