Sublime Text is an editor which has the balance between simplicity and power. Many programmers are using Sublime for their daily programming.
By default, Sublime can compile a Java source file as long as the Java compiler (javac) can be found via the PATH environment variable. So make sure you update this variable so Sublime can interact with the Java compiler.
To compile a Java source file from within Sublime, press Ctrl + B (or click Tools > Build). If the Java compiler couldn’t be found, Sublime will display an error message like this:
[Error 2] The system cannot find the file specified
Once you corrected the PATH variable, you may need to restart the editor to take effect. Then press Ctrl + B again, you would see the successful message like this:
To run the compiled class, you need to do some extra steps (on Windows):
1. Create a batch script file called runJava.bat with the following content:
@ECHO OFF cd %~dp1 ECHO Compiling %~nx1....... IF EXIST %~n1.class ( DEL %~n1.class ) javac %~nx1 IF EXIST %~n1.class ( ECHO -----------OUTPUT----------- java %~n1 )
The purpose of this script is to call Java compiler (javac) to compile the source file. Then if the compilation succeeds (identified by checking if the .class file generated), call the Java launcher (java) to run the program.
2. Save the runJava.bat under JDK’s bin directory, e.g. c:\Program Files\Java\jdk1.8.0\bin.
3. Locate and open the JavaC.sublime-build file under this directory: c:\Users\YourName\AppData\Roaming\Sublime Text 2\Packages\Java. You would see the content of this file like this:
Replace the text “javac” by “runJava.bat”:
Then press Ctrl + B again, you would see the following result:
NOTE: If you are on a Linux system, create the following shell script file:
[ -f "$1.class" ] && rm $1.class for file in $1.java do echo "Compiling $file........" javac $file done if [ -f "$1.class" ] then echo "-----------OUTPUT-----------" java $1 else echo " " fi
Save this file as runJava.sh under JDK’s bin folder, and update the JavaC.sublime-build file by replacing “javac” by “runJava.sh”.
Here's the Java source file:
public class HelloJava { public static void main(String[] args) { System.out.println("Hello Java!"); } }