Identify Java version of JDK

6

I am creating an IDE and when the user presses to run the code I do the following:

try {
    File file = new File(arquivoSelecionado.nome);
    try {
        FileWriter fw = new FileWriter(file);
        fw.append(code.getText());  
        fw.close();

    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
    Runtime.getRuntime().exec("C:\Program Files\Java\jdk1.8.0_31\bin\javac "+file.getName());
    Thread.sleep(2000);
    Process pro = Runtime.getRuntime().exec("cmd /c start java "+file.getName().replaceAll(".java", ""));   

} catch (Exception e1) {
    e1.printStackTrace();
}

As you can see I'm putting a fixed path to the jdk folder, in this case the version is the one found on my computer, the problem is that when the jdk version is different from the one on the path fixed it does not run the terminal by running the program

What I wanted to know is if you have any way to identify the version of jdk where the IDE is running

    
asked by anonymous 30.05.2018 / 05:58

1 answer

3

Perhaps a more efficient way is to inspect the JAVA_HOME environment variable:

String javaHome = System.getenv("JAVA_HOME");

The String that returns from this method brings various information, including the path where the compiler is installed.

Just be aware that if the computer running your program does not have Java installed, the javaHome variable will be null.

    
30.05.2018 / 13:34