compile java programs in sublime text 2

Sublime Text 2 is the latest craze in text editors, and it can be customized to such extent that it has the potential to perform as a light-weight IDE. Who wants to launch Eclipse to create a Hello World program (or say a programming assignment at an online course)? Sublime Text 2 is the answer. Here is what we need to do to get started quickly:

1. Include JDK/bin in System PATH:
Default location of JDK in Windows 7 is C:\Program Files (x86)\Java\jdk1.7.0_13\bin.
Add this location (or whatever it is in your case) at the end of current System PATH (if other JDKs are installed, put the one to be used before others in the PATH).

64 bit JDK
For 64 bit JDK, we need to put the corresponding JDK/bin instead (default: C:\Program Files\Java\jdk1.7.0_13\bin). Ensure that the JRE and JDK versions match, else the compiled classes would not run. To know your JRE version and whether 32/ 64 bit, open a cmd prompt and type java --version.

If both 64 bit and 32 bit JREs co-exist in a system, then by default it selects 32 bit JRE to run the application. So if we use 64 bit JDK to compile the code and we have a lower version 32 bit JRE also installed, then the compiled class will be run on this older JRE and we’ll get $#^&@%!*!&^*@. Better is to disable older JREs if not required.

Sublime Text 2 comes with build config included for most popular languages like C++/ Java, as long as the compiler locations are included in System PATH. These configuration files use JSON like key-value pairs and are found in Preferences -> Browse Packages folder (in Windows 7 it is at C:\Users\user\AppData\Roaming\Sublime Text 2\Packages). Any build config file can be found in the respective language folder here. So for example, the java build config file is in C:\Users\user\AppData\Roaming\Sublime Text 2\Packages\Java\JavaC.sublime-build.

The java build config file has by default 3 key-value pairs as shown below:

"cmd": ["javac", "$file_name"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": ""

Therefore when we set Tools -> Build System to JavaC and run Tools -> Build/ Ctrl+B from a source (say, the command invoked is javac, i.e. the java compiler is called on the current file name ( If the JDK/bin location is included in System PATH, then javac will compile that file.

2. Write your own batch file to build all source files in the current directory:
We can write our own batch file and call that instead of javac, and in this batch file we can specify any customized build config we want to build and run our project with. For basic compiling and running java code, we can create a batch file say runJavaST2.bat (Windows/ MS-DOS) as below:

ECHO Compiling %~nx1.......
IF EXIST *.class (
DEL *.class
javac *.java
IF EXIST %~n1.class (
ECHO -----------OUTPUT-----------
java %~n1

This file takes in an argument that it refers as nx1 (1: first arg). When Ctrl+B /Tools -> Build is invoked, the current file is passed as argument to our batch file and compilation starts. The class in this file is also the entry point of our executable. We refer to the full name of file with nx1, and excluding extension with n1 (i.e. without the “x”). So the first line will echo “Compiling” on the output panel. Next we check for all existing class files (the wildcard ‘*’ ensures this) in the same folder and if exist, delete all of them. Then we use javac to compile/ rebuild that directory. This will create .class files for all .java files. This ensures the second condition passes and now we run MyClass.class file using java command. We don’t need to specify extension (just class name is sufficient), so we use the var n1 instead of nx1.

3. Put your batch file in JDK/bin directory
All we need now is to call our batch file instead of javac in the build config. We copy the batch file to the same location as javac (i.e. JDK/bin path: C:\Program Files (x86)\Java\jdk1.7.0_13\bin) and modify JavaC.sublime-build to:

"cmd": ["runJavaST2.bat", "$file_name"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": ""

This means the batch file (with 1 argument: the complete file name) will be called when we invoke Ctrl+B/ Tools -> Build from our current file. You might need to restart Sublime Text 2. Since the JDK/bin is in our System PATH, it will run the batch file and show us the output in output panel at the bottom.

4. Code and run
Now for the win: On my machine, Sublime Text 2 takes around 15 MB RAM with ~10 files open. Ditto on Eclipse is ~250 MB.

For Linux:
Everything is same except that PATH is appended differently and the batch file is a “shell script”, i.e. with extension .sh. The script may need to be chmod a+x to give it permission to execute. Below is a sample script (taken from here):

# compiles all java files within directory and runs first argument
for file in *.java
echo "Compiling $file"
javac $file
echo "Running $1"
java $1

Further customizations
Want to use Ant? Need to pass JVM arguments? Anything can be scripted in the batch file and achieved with Ctrl+B. For other languages? Just modify the corresponding build file with that language-specific build instructions, then change the build system. Another FTW.


9 thoughts on “compile java programs in sublime text 2

  1. You DO Not Need TO Make A New Batch File TO Compile And Execute The .java File
    You Can Use This |<>| file to automatically compile & run your file !! This Way It Is Portable !! 😀 :-

    “cmd”: [“javac”,”$file”,”&”,”java”,”-cp”,”$file_path;”,”$file_base_name”],
    “file_regex”: “^(…*?):([0-9]*):?([0-9]*)”,
    “working_dir”: “${file_path}”,
    “selector”: “”

  2. It works nice with non-packaged classes. With package-scoped classes, it doesn’t. Have you solved this one already?

  3. hai,
    great post . thank u for explaining . I did all things in the above post. Strangely, it compiles ALL .java files in a particular folder . how can i rectify it ?

      1. Hi,

        You can modify the batch file to compile only the class that you want instead of using the ‘*’ wildcard (for which it looks at all the files).

  4. Hello, Neat post. There’s a problem together with your web site in web explorer, might check this? IE still is the market chief and a huge element of people will leave out your great writing due to this problem.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s