Blog

Enjoying life through Art, Code, and Nature.

Sublime Text 2 Compile and Run Java Code

Sublime text 2 is a great text editor.  What makes it even better is the ability to compile and run Java code.  It seems that there is a lot of confusion on how to get Java to compile and run on Windows machines using Sublime Text 2.  So in short, here is my first tutorial:

1) Install sublime text 2 here.

2) Install the JDK (Java Developers Kit) from here.

3) After the JDK has been installed, locate the bin folder path in the JDK install directory.  Mine is”C:\Program Files\Java\jdk1.6.0\bin\”.

4)  You will now need to modify the path value to prevent any syntax errors from occuring.  To do this, add double slashes where there is a single slash in the JDK bin path.  Ex:   C:\\Program Files\\Java\\jdk1.6.0\\bin\\.

5) Open Sublime Text 2 and go to Preferences > Browse Packages

6) In the browser window that popped up go to the Java folder

7) Open the file JavaC.sublime-build and replace all the code in the file with the code below:

{
 "cmd": ["javac", "$file_name","&&","java", "$file_base_name"],
 "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
 "path": "C:\\Program Files\\Java\\jdk1.6.0\\bin\\",
 "selector": "source.java",
 "shell": true
 }

8) Make sure you substitute the path with your own JDK bin path.  Don’t forget to save your file.

9) In Sublime Text 2 select Tools > Build System > JavaC.  Confirm that JavaC has a check mark next to it.

10) Open up your java file and compile and run it by going to Tools > Build. You can use the shortcut ctrl+b.

If this article was helpful, please feel free to write a comment below.

  • http://www.facebook.com/rupa.rajagopalan.1 Rupa Rajagopalan

    Awesome tutorial, thanks for posting. You really helped me out

    • swscreative

      Thanks!

  • Sammy Guergachi

    I get a java.lang.NoClassDefFoundError when I run a simple main.java thing

    • Guest

      Sammy, your error is beyond the scope of this tutorial as it seems that your code is compiling. The error you are having is that your Class definition was found at compiled time but is not found at run-time. This resource might help you. http://javaeesupportpatterns.blogspot.com/2012/06/javalangnoclassdeffounderror-how-to.html

    • swscreative

      Sammy, your error is beyond the scope of this tutorial as it seems that your code is compiling. The error you are having is that your Class definition was found at compiled time but is not found at run-time. This resource might help you. http://javaeesupportpatterns.blogspot.com/2012/06/javalangnoclassdeffounderror-how-to.html

    • Aaron

      If you are using any sort of package declaration, you will get this error. AFAIK, there is no way to inject the fully qualified class name into the Sublime build system. For class in the default package, the plugin seems to work.

      In order to build without trying to run the class change the top ‘cmd’ line to :

      “cmd”: ["javac", "$file_name"],

      Then you won’t get the error, but you’ll have to open a shell to run your main method.

  • Daisy

    You are awesome, this really helps. It really works

    • swscreative

      Im happy that it is working for you Daisy.

  • Ciaran O’Connor

    Should this work for mac osx aswell?

    • swscreative

      This has only been tested on windows. Feel free to test it on mac. Obviously, the paths will be different.

      • Taran Pabla

        I have it working on mac using this config

        {

        “cmd”: ["bash", "-c", "javac ${file} && java ${file_base_name}"],

        “file_regex”: “^(…*?):([0-9]*):?([0-9]*)”,

        “selector”: “source.java”

        }

        • Evian

          Thanks for sharing your awesomeness! Swsadmin!

          By the way, I am using Sublime text2 on a mac. As your advice, I replaced the path accordingly. However I am not getting the outcome I expected. Can anybody help me out?

          {
          “cmd”: ["javac", "$file_name","&&","java", "$file_base_name"],
          “file_regex”: “^(…*?):([0-9]*):?([0-9]*)”,
          “path”: “/Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/Home/bin”,
          “selector”: “source.java”,
          “shell”: true
          }

          The outcome is as follows.

          Usage: javac
          where possible options include:
          -g Generate all debugging info
          -g:none Generate no debugging info
          -g:{lines,vars,source} Generate only some debugging info
          -nowarn Generate no warnings
          -verbose Output messages about what the compiler is doing
          -deprecation Output source locations where deprecated APIs are used
          -classpath Specify where to find user class files and annotation processors
          -cp Specify where to find user class files and annotation processors
          -sourcepath Specify where to find input source files
          -bootclasspath Override location of bootstrap class files
          -extdirs Override location of installed extensions
          -endorseddirs Override location of endorsed standards path
          -proc:{none,only} Control whether annotation processing and/or compilation is done.
          -processor [,,...] Names of the annotation processors to run; bypasses default discovery process
          -processorpath Specify where to find annotation processors
          -d Specify where to place generated class files
          -s Specify where to place generated source files
          -implicit:{none,class} Specify whether or not to generate class files for implicitly referenced files
          -encoding Specify character encoding used by source files
          -source Provide source compatibility with specified release
          -target Generate class files for specific VM version
          -version Version information
          -help Print a synopsis of standard options
          -Akey[=value] Options to pass to annotation processors
          -X Print a synopsis of nonstandard options
          -J Pass directly to the runtime system
          -Werror Terminate compilation if warnings occur
          @ Read options and filenames from file

          [Finished in 0.2s with exit code 2]

          end

  • stephie

    Thank you so much, it really helped!!!

  • pri

    Excellent!! Thank you!! ^.^

  • Luke Hill

    I’m getting the Could not find or load main class error. I have the path changed to my path with the double slashes but it’s still not working.

  • Max Masixole

    Hello , Thanks for the article … May God bless you

  • Marc_Online_

    Thanks man, it even works with Sublime Text running on Linux. :-)

  • Hugo

    Thanks!!! helped me…

  • Daniel

    Wow! You’re the best! :)

  • Colin Mimokatito Thomas

    I cant open the file

  • Tori

    Awesome! It worked. thanks!

  • Renee

    Yay it worked! Thanks!

  • Mike

    I’m getting an error “Error trying to parse build system: Invalid escape in “dir/RoamingSublime Text 2PackagesJavaJavaC.sublime-build4:10″

    Here is what I copied for the JavaC.sublime-build file:

    {
    “cmd”: ["javac", "$file_name","&&","java", "$file_base_name"],
    “file_regex”: “^(…*?):([0-9]*):?([0-9]*)”,
    “path”: “C:Program FilesJavajdk1.7.0_25bin”,
    “selector”: “source.java”,
    “shell”: true
    }

    any thoughts? :/

    • swscreative

      Make sure there are double slashes in your path:

      C:Program FilesJavajdk1.7.0_25bin

      should be:

      C:\Program Files\Java\jdk1.7.0_25bin\

  • bumblebee

    Great job ! thank you!

  • Derek

    Thanks a bunch! Only solution i’ve found so far!

  • http://ashudave.tumblr.com/ Ashutosh Dave

    It was working earlier when I had JDK 7 but now when I removed JDK7 and installed JDK6, its not working. I get this error
    “javac’ is not recognized as an internal or external command,
    operable program or batch file.”

    I did change the path value in environments and sublime text, It still gives the same error. Please help!

  • Theerawat

    Thanks for the awesome topic !

  • Tyler

    Works great! Thanks a bunch.