Identify JDK version by Java

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();
}

Only as it is possible to see I am putting a fixed path to the JDK folder, in this case the version is the one that is on my computer, the problem is that when the version of jdk is different from that in the fixed path it does not run the terminal running the program

What I wanted to know is if there is any way to identify the version of the jdk in which the IDE it's rolling

Author: Gustavo Balero, 2018-05-30

1 answers

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 up various information, among them, the path in which the compiler is installed.

Just pay attention to the fact that if the computer running your program does not have Java installed, the variable javaHome is null.

 3
Author: StatelessDev, 2018-05-30 11:34:51