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 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 

#37Sakthivel2024-10-09 19:56
How to run file download
Quote
#36sayed2024-09-13 05:55
Not working on ubuntu. I need on ubuntu terminal will run in sublime text. not in another window.
Quote
#35Jhon2024-05-15 00:13
That's Working Man. I spend So Many hours thanks Lot
Quote
#34Dalpat kumar2024-04-23 20:45
Good morning hello world
Quote
#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