1

I have an application and an installer for this application, both written in Java.

During the installation process the user has to define the path of that Java's Home he later wants to use for the application runtime. This could be another JRE than the one which my installer currently run and also doesn't necessarily have to be the JAVA_HOME path.

So, to get the version of the user defined target Java where the actual application should run, I cannot simply call System.getProperty("java.runtime.version") in my installer code because this give me the version of JRE currently running the installer.

Is there any common way to programmatically get the Java version of a JRE when I only have the path to that JRE? Maybe with a separate class loader? Or with reading any META-INF or reading any class within that JRE lib?

1
  • 2
    One option is to use ProcessBuilder and run that java process with -version then parse the output... Commented Dec 12, 2018 at 12:12

2 Answers 2

3

Okay after reading your comments I have wrote code that executes java -version

ProcessBuilder pb = new ProcessBuilder("java", "-version");
pb.directory(new File(System.getProperty("user.home")));
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String javaVersion = reader.readLine();
System.out.println(javaVersion.substring(javaVersion.indexOf("\"") + 1, javaVersion.lastIndexOf("\"")));

You can also call it with the EXACT path

ProcessBuilder pb = new ProcessBuilder("C:\\Program Files\\Java\\jdk1.8.0_181\\bin\\java", "-version");

this will output the java version as 1.8.0_181

Thanks to Andreas Hauschild that pointed out release file contains the version I wrote code to parse the version from that file so use this. If you don't wanna execute commands and want to get the java version from release file I wrote a code to just parse the version from it

public static String getJavaVersion(String javaPath) throws IOException {
    File releaseFile = new File(javaPath, "release");
    BufferedReader bufferedReader = new BufferedReader(new FileReader(releaseFile));
    String version = bufferedReader.readLine();
    return version.substring(version.indexOf("\"") + 1, version.lastIndexOf("\""));
}

You can call it by using

String version = getJavaVersion("C:\\Program Files\\Java\\jdk1.8.0_181");
Sign up to request clarification or add additional context in comments.

Comments

2

If you have the install directory of Java you can read the "release" File.

It located in the root directory of the installation (e.g.):

C:\Program Files\Java\jre1.8.0_191\release
C:\Program Files (x86)\Java\jre1.8.0_191\release

the content looks like:

JAVA_VERSION="1.8.0_191"
OS_NAME="Windows"
OS_VERSION="5.2"
OS_ARCH="amd64"

enter image description here

6 Comments

That's a good hint. But in my case I have not that "release" file in any of my local JRE ... where does it came from? EDIT: Ah, sorry, I looked in my JDK's JRE. In that case the release file is located in JDK's directory. Okay, this could be an answer for me.
I cross checked it on linux and it is also located at the place that i mentioned.
Why down-voting this answer?
I think some dont understand that you can't just call "java -version", because this returns only the version which is set under JAVA_HOME/PATH. I would just read the file before calling system commands.
Yes i know and if the installing user is not allowed to execute the file (e.g. Linux)? Your way is also correct, but reading the file will maybe worke more often asy trying to execute java.sh/exe
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.