Home Download Buy Blog Forum Support

Compiling and Running Java

Compiling and Running Java

Postby JailBot on Sat Jan 21, 2012 10:14 am

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?
JailBot
 
Posts: 2
Joined: Sat Jan 21, 2012 10:09 am

Re: Compiling and Running Java

Postby C0D312 on Sat Jan 21, 2012 12:32 pm

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: http://www.sublimetext.com/forum/viewtopic.php?f=3&t=1685

You should also check out my Display Functions (Java) plugin :)
Available here: https://github.com/BoundInCode/Display-Functions
or through Package Control
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Compiling and Running Java

Postby JailBot on Sat Jan 21, 2012 9:42 pm

So i've put the bat file in C:\Program Files\Java\jdk*\bin\
Code: Select all
@ECHO OFF
cd %~dp1
javac %~nx1
java %~n1

and named it javacexec.bat

then I edited JavaC.sublime-build so that it says
Code: Select all
{
   "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
Code: Select all
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.
JailBot
 
Posts: 2
Joined: Sat Jan 21, 2012 10:09 am

Re: Compiling and Running Java

Postby bunnyDrug on Fri Mar 09, 2012 10:29 pm

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
    Code: Select all
    ;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 :roll:
As for running, I just use the CMD line at the moment.
Hope it helps!
bunnyDrug
 
Posts: 1
Joined: Fri Mar 09, 2012 10:19 pm

Re: Compiling and Running Java

Postby kaitagsd on Sat Jul 20, 2013 9:24 pm

This worked for me, coupled with the exec bat method. Thanks!!!!!!!!!!!!!
kaitagsd
 
Posts: 1
Joined: Sat Jul 20, 2013 9:15 pm

Re: Compiling and Running Java

Postby MrReview on Mon Jul 22, 2013 3:07 pm

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.

Code: Select all
@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
)
MrReview
 
Posts: 1
Joined: Mon Jul 22, 2013 3:02 pm

Re: Compiling and Running Java

Postby psychowico on Mon Dec 23, 2013 8:56 pm

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.
psychowico
 
Posts: 1
Joined: Mon Dec 23, 2013 8:47 pm

Re: Compiling and Running Java

Postby neromare on Fri May 09, 2014 1:06 pm

psychowico wrote: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.


this is fantastic! Also like the fact that you also mentioned how to configure the keyboard short cuts.
Thanks
neromare
 
Posts: 1
Joined: Fri May 09, 2014 1:03 pm

Re: Compiling and Running Java

Postby iamcreasy on Thu Jul 03, 2014 7:05 am

iamcreasy
 
Posts: 1
Joined: Thu Jul 03, 2014 3:25 am


Return to Technical Support

Who is online

Users browsing this forum: Google [Bot] and 37 guests