Sublime Text is a very popular editor for writing code. For Java, it supports compiling a Java source file with the default build named JavaC. Click Tools > Build System > JavaC to set the default build type for Java:

Sublime Set Default Build

Then you can compile the current Java source file by clicking Tools > Build or press the shortcut key Ctrl + B.

NOTE: To be able to compile and run Java source files in Sublime, the Java compiler (javac) and Java launcher (java) programs must found in the PATH system variable. See: How to set environment variables for Java using command line

If there’s a compilation error, Sublime captures and displays the Java compiler’s output right inside the editor like this:

Sublime Compile Java Error

The .class file is generated in the same folder as the source file.

In case the compilation is successful, Sublime simply displays the following message:

Sublime Compile Java Success



 

However, Sublime Text 3 doesn’t have build-in support for running a Java program. So to run a Java source file, we need to configure the build system a little bit.

Click Tools > Build System > New Build System… And in the untitled.sublime-build editor, type the code as follows:

New Java Build

Save this file as RunJava.sublime-build in the folder prompted by Sublime. Then you could see the new build appears in the menu like this:

New Build Added

 

Now, to run the current Java source file, set the default build as RunJava and press Ctrl + B. Sublime captures and displays the program’s output right inside the editor like this:

Sublime Java Output

 

If you want to compile and run a Java program in just a single keystroke, modify the RunJava.sublime-build file as follows:

{
	"shell_cmd": "javac $file && java $file_base_name"
}
Save the file. Now, press Ctrl + B and you will see the magic happens: the current Java source file is compiled and then executed (if no compilation errors).

That’s how to compile and run a Java program with Sublime Text 3 in simple way. Happy coding!

 

You can also watch this video to learn how to use Sublime Text editor to compile and run Java program:

 

Related Java Editors / IDEs Tutorials:

 

Other Java Coding Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment

   


Comments 

#33Rizal2023-03-02 10:05
That's exactly what i did. But what if i want to do the same thing with a java package? As you know, .class files are inside sub-folders in java packages, so it wouldn't work in this case.
Quote
#32Mohammed2022-09-18 05:00
very useful information
Quote
#31KilluaV2022-07-13 07:34
thanks for help, you are my buddy
Quote
#30David Suarez2022-03-25 10:55
Thanks, works for me
Quote
#29Muneeshwaran2022-03-13 20:23
Send the sublime code
Quote