Configuring Sublime Text 2 editor to compile and run Java programs
- Details
- Written by Nam Ha Minh
- Last Updated on 02 July 2019   |   Print Email
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!"); } }
Related Java Editors / IDEs Tutorials:
- How to compile and run a Java program with Sublime Text 3
- 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
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.