Compiling and Running Java Code in Sublime Text 2

So this is what i added to the JavaC.sublime-build file

{
    "cmd": ["javac", "-Xlint", "$file"],
    "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
    "selector": "source.java",

    "variants": [

        { "cmd": ["javac", "-Xlint", "$file"],
          "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
          "selector": "source.java",
          "name": "Java Lintter"
        },  

        { "cmd": ["java", "$file_base_name"],
          "name": "Run Java"
        }
    ]
}

What this does is that it creates variants to the regular build command (ctrl+b). With ctrl+b you will still be able to compile your code. If you do shift+ctrl+b the first variant will be executed, which in this case is javac with the -Xlint option. The second and final variant is the java command itself. you can place this as your first variant and shift+ctrl+b will actually execute the java code.

Also, notice that each variant as a “name”. This basically allows this specific “build” option to show up in the shift+ctrl+p option. So using this configuration, you can simply do shift+ctrl+p and type “Run Java” and hit enter, and your code will execute.

Hope this helped.

Leave a Comment