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

javac not found

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:

finished compile

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:

JavaC build script

Replace the text “javac” by “runJava.bat”:

runJava bat

Then press Ctrl + B again, you would see the following result:

Run Java program

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!");
	}
}

 

Related Java Editors / IDEs Tutorials:

 

Other Java Coding Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment

   


Comments 

#2Fred Buecker2017-08-16 12:08
Note: On an OS like Windows 10, be sure to run your ZIP viewer as Administrator or you will not be able to save the file back into the package!
Quote
#1Fred Buecker2017-08-16 11:55
This process can be followed for Sublime 3, but note should be taken that the Packages are now in Program Files:

C:\Program Files\Sublime Text 3\Packages

The Java package is called "Java.sublime-package".

These ".sublime-package" files are actuallu just .ZIP files with a different extension. I used my ZIP package viewer, 7ZIP, to open the archive and gain access to the JavaC.sublime-build file so you can add the runJava.bat statement.
Quote