简体   繁体   中英

How can I change the path to Java in Sublime?

I am using an office laptop and I do not have administrative rights to it therefore I cannot edit the environment variables.

Recently, my laptop auto updated to Java 8 but something got goofed up and when ever I try to build a java program I get an error. In the meantime, I would like to build and run Java programs on Sublime text using my functioning Java 7.

How can I change the path in Sublime Text to Java 7 instead of it defaulting to Java 8 when I press CTRL-B?

Steps to use java in sublime: In sublime go to Tools -> Build System -> New Build System put below code:

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

Save it with any name (like, Runjava.sublime-build)

Note: In path give whatever java version present in your system at these location "C:\\Program Files\\Java\\jdk-14.0.2\\bin\\"

Then, create a folder with three files in it B.java(Main java file), input.txt, output.txt. Go to Tools -> Build System -> Select Runjava

put below code in B.java file:

import java.io.*;

import java.util.*;

public class B {

    public static void main(String[] args) {

        try {

            System.setIn(new FileInputStream("input.txt"));

            System.setOut(new PrintStream(new FileOutputStream("output.txt")));

        } catch (Exception e) {

            System.err.println("Error");

        }
        
        System.out.println("Hello World!");

    }
}

give input in input.txt file

see output in ouput.txt file

You could try to make a special build system JavaC7 or something like that:

{
    "shell_cmd": "/absolute/path/to/javac \"$file\"",
    "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
    "selector": "source.java"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM