Search code examples
javasublimetext3

Setting Sublime Text 3 to run specific Java version


I have Java 11 and 7 installed since I am maintaining a website that uses Java 7. Some IDE now only support Java 11 onward, so I was figuring out how to set Sublime Text 3 to specifically run Java 7, without changing the default Java 11 on my system. I manage to figure it out by putting multiple pieces together, so I am documenting the answer here for other's reference.


Solution

    1. On Sublime Text 3, go to "Tools" > "Build System" > "New Build System..."
    2. Paste the following piece of code. Edit the path setting accordingly. I'm using Zulu JDK.
    {
        "cmd": ["javac", "$file_name", "&&", "java", "$file_base_name"],
        "selector": "source.java",
        "file_regex": "^\\s*File \"(...*)\", line ([0-9]*)",
        "path": "C:\\Program Files\\JDK-Zulu\\zulu-7\\bin",
        "shell": true
    }
    
    1. Save the setting with extension .sublime-build. I named mine Java7 Run.sublime-build.
    2. After saving, it should appear in your Build System list. Select that and now when you Build, it will use Java 7 (or whatever Java version you set it to be).

    To validate, you can use System.out.println(System.getProperty("java.version"));