How to compile and run a Java program with Sublime Text 3
- Details
- Written by Nam Ha Minh
- Last Updated on 02 July 2019   |   Print Email
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:
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:
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:
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:
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:
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:
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:
- How to Compile and Run a Java Program with TextPad
- How to use Eclipse IDE for Java EE Developers
- How to Use NetBeans IDE from the Basics
Other Java Coding Tutorials:
- 10 Common Mistakes Every Beginner Java Programmer Makes
- 10 Java Core Best Practices Every Java Programmer Should Know
- How to become a good programmer? 13 tasks you should practice now
Comments