3

I need to get the java version from my application that the user has currently installed on their machine. However, the application that I have is installed with a self-contained JRE and when I do a System.getProperty("java.version"), it only returns the version of that self-contained JRE. Is there anyway that I can get the version that is installed on the machine?

Greg Abel
  • 49
  • 1
  • 3
  • possible duplicate of https://stackoverflow.com/questions/10613359/how-to-check-jre-version-using-java-application – Jabongg Jan 07 '19 at 11:33
  • Possible duplicate of [how to check jre version using java application](https://stackoverflow.com/questions/10613359/how-to-check-jre-version-using-java-application) – Raduan Santos Jan 07 '19 at 11:42
  • *"the version that is installed"* There may be multiple versions installed. But this sounds like an XY problem. See [What is the XY problem?](http://meta.stackexchange.com/q/66377) – Andrew Thompson Jan 08 '19 at 02:48

6 Answers6

3

The JRE installation are listed in the registry in the key only for Windows, Linux do not have central registry.

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment

You can make a simple program to test :

public class ShowVersion {
 public static void main(String args[]) {
   System.out.println(System.getProperty("java.version"));
 }
}

Or you can try command prompt

Type java -version

For more you can refer sister site of SO :

https://superuser.com/questions/1221096/how-do-i-check-what-version-of-java-i-have-installed

Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55
  • Is that info also in the registry on linux systems ? – Erwin Smout Jan 07 '19 at 11:59
  • @ErwinSmout Linux doesn't have a centralized registry like Windows does. Idk why you asked it AND DOWNVOTED IT – Vishwa Ratna Jan 07 '19 at 12:02
  • Because I already knew that and it makes this answer a poor one. At the very least you might have been explicit about the fact that such hacks make a program OS-dependent, which breaks one of the most important reasons why one would use java. – Erwin Smout Jan 07 '19 at 12:07
  • @ErwinSmout but CMD `java -version` will still give the info of version installed on machine. You can always find the location where yoiur Java is installed by `$ whereis java` – Vishwa Ratna Jan 07 '19 at 12:14
  • It won't if the java executable isn't on the OS's search path. Which it won't if I simply unzipped an archive and didn't bother to put a symlink in /usr/sbin or some such. The broader issue is that answering the question here is less helpful than pointing out why it shouldn't even have been asked. – Erwin Smout Jan 07 '19 at 12:30
  • Edited my question and added Windows explicitly to it. Hope it is fine now. ANyways thanks for pointing it out, learnt today that answers must be OS independent :) – Vishwa Ratna Jan 07 '19 at 12:40
  • The information in the registry is only populated if you use the Oracle Java installer on Windows, it is not the case if you use a different distribution (eg OpenJDK, AdoptOpenJDK, J9, etc). – Mark Rotteveel Jan 08 '19 at 17:13
1

if you're bundling a JRE (did you check the distribution license for it? Make sure you are allowed to do so) and it's running under that, you get that version back.

If the user were to run it under another JRE, you'd get the version of that JRE. That's just how things work.

In fact if you're using a self-contained JRE the user doesn't even have to have another JRE installed on his system at all, that's the entire point of bundling one in the first place.

Apart from a full file system scan it's impossible to know what other JVMs might be installed, and if you do that you'd have to account for all the different names the Java runtime executables may have depending on the files system you're running on. And after finding all those executables you still have no real way of knowing what version of Java they belong to unless you either do a binary analysis of the executables or somehow detect the information from other files in the directories where those executables are installed, files that may or may not be present depending on the system in question, how it was set up, what JVM is in use, and whether the installation has been manually altered or not.

For example, in the root directory of a JDK installation there is a file called "release" which contains the JVM version, but AFAIK that file isn't required to be there for the JVM to work properly. And your application may not have the rights to the file system to read that file.

jwenting
  • 5,505
  • 2
  • 25
  • 30
0

You can try looking in known paths e.g. C:\Program Files\Java but unless you scan the entire file system for java or java.exe you will never be sure.

This might not work as expected as in the user that runs your application shouldn't have access to system directories. You can force you application to be started with administrator level access but that would be a security risk.

You requirement seems a bit pointless if you are already bundling a JRE with your application.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • +1 for the last two paragraphs but -1 for the first. In general, I'm often baffled by how easily responders here make an asker introduce OS dependencies in their programs, when the whole point of java was OS independence to boot. – Erwin Smout Jan 07 '19 at 12:03
0

You can run cmd command using java (java -version) and use that output to get version.

    List<String> cmdArgs = new ArrayList<>();   

                        cmdArgs.add("cmd.exe");
                        cmdArgs.add("/C");
                        cmdArgs.add("java -version");

    ProcessBuilder processBuilderObj = new ProcessBuilder();
                    processBuilderObj.command(cmdArgs);
      Process process = processBuilderObj.start();
 //get inputStream 
                        BufferedReader data = new BufferedReader(new InputStreamReader(process.getInputStream()));
                        String line1;
                        while ((line = data.readLine()) != null){
                            System.out.println(line);
                        }
  • Maybe you just need to run "java -version" rather than "javac ...", because the question is concerned about JRE version, not JDK version. – elyor Jan 07 '19 at 12:07
  • This works only on windows as you are using "cmd.exe", but you don't really need that. – elyor Jan 07 '19 at 15:26
  • we can use System.getProperty("os.name") to get os name and on basis of that if it is windows we can run this if it is linux we can run linux command to get version. – rishav prasher Jan 08 '19 at 07:59
0

While System.getProperty("java.version") returns the version of the Java that your app is running on, calling "java -version" as a system command from your Java app will return the version of the default(installed) Java in your system.

Here are the generic steps to find it programmatically:

  1. Using ProcessBuilder in Java, execute the system command "java -version". This is certainly platform-independent.
  2. Read the output from executed process. The version is usually on the 1st line of the output (smth like java version "1.8.0_191"), so reading the 1st line is enough.
  3. Check using regular expression, if the output matches a string containing a java version, you just parse the version and that will be what you are looking for.
  4. Otherwise, it means the output is an error message (smth like -bash: java: command not found), and there is no Java installed in your system.
elyor
  • 998
  • 9
  • 20
0
  1. control panel
  2. java
  3. Java tab
  4. view you can see exact version of java used and it's location.