Check Java Version on Multiple Installed JDKs

  • Java
  • Thread starter rathodnileshn
  • Start date
  • Tags
    Java
In summary: Modern javaw is a wrapper around java.exe, but it's still possible to run a specific version of java.exe, say 1.6.0_06, by using the -Djava.version=1.6.0_06 flag.In summary, Nilesh Rathod says that using System.getProperty("java.lversion") in Java code will return the version of the JVM that is running, not the value of the JAVA_HOME environment variable.
  • #1
rathodnileshn
2
0
Hi all,

Suppose on computer 4 different versions of java are installed.
(1) Depending on the path of java.exe we can open command prompt go to path /jdk_version/bin/java.exe and type "java -version" and know the
particular version, But we can't extract the output of the command "java -version" into a file.

(2) Also using System.getProperty("java.lversion"); in java code we can get java version but it will return the version of the jvm that is running that
program. Or the path set to environment variable JAVA_HOME.
(3) Also following code can be used but it's not working:!(don't know why??)
import java.io.*;
import java.lang.*;

public class x
{

public static void main (String args[])
{
try
{
// get runtime environment and execute child process
Runtime systemShell = Runtime.getRuntime();
Process output = systemShell.exec("java -version");
// open reader to get output from process
BufferedReader br = new BufferedReader (new InputStreamReader(output.getInputStream()));
String line = null;
System.out.println("<OUTPUT/>");
while((line = br.readLine()) != null )
{ System.out.println(line); } // display process output
System.out.println("</OUTPUT>");
int exitVal = output.waitFor(); // get process exit value
System.out.println("Process Exit Value : "+ exitVal);
}
catch (IOException ioe){ System.err.println(ioe); }
catch (Throwable t) { t.printStackTrace();}
}
}
*** My requirement is :***

4 or more jdk are installed on OS. depending on path of java.exe I need to find it's version.

Can anybody help please?


Thanks in advance,
Nilesh Rathod.
 
Technology news on Phys.org
  • #2
First, I believe the copy of java that is executed is in c:\windows\system32\java.exe, which forms part of the JRE. You can verify this for your computer using a utility called "which" that is a translation to Windows of the same command in Unix.
This is what I get when I type "which java":
C:\WINDOWS\system32/java.exe

Then,
>> ... But we can't extract the output of the command "java -version" into a file.
Assuming all you need to know is to find out which version of Java your current JRE is using, you can capture the output of
java -version
by the command:
java -version 2>filename

The 2 is required before the redirection operator > because java.exe directs its output to stderr and not stdout.

example output on my computer:
java version "1.6.0_05"
Java(TM) SE Runtime Environment (build 1.6.0_05-b13)
Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode, sharing)

Hope that sovles your problem.
 
  • #3
Hey,

Thank you very much!
You solved my problem!

Thanking you,
Nilesh Rathod.
 
  • #4
Glad to be of help!
Come and visit us often!
 
  • #5
rathodnileshn said:
(2) Also using System.getProperty("java.lversion"); in java code we can get java version but it will return the version of the jvm that is running that
program. Or the path set to environment variable JAVA_HOME.

System.getProperty will return the version of the JVM that is running, not the value of the JAVA_HOME environment variable.

It is possible to get the value of JAVA_HOME, if you really want that, but not through that call. Also, mathmate's advice about using which is excellent. Finally, it's usually desirable to make sure that the executable pointed to in your PATH corresponds to the version specified in JAVA_HOME, or else tools like Ant may get confused.

I'm not 100% sure that this is still true, but in the olden days, you should prefer to run javaw on a windows machine, and not java.
 

What is the purpose of checking Java version on multiple installed JDKs?

The purpose of checking Java version on multiple installed JDKs is to ensure compatibility of Java programs with the installed JDKs and to identify any potential issues or updates that may be needed to run the programs successfully.

How can I check the Java version on multiple installed JDKs?

You can check the Java version on multiple installed JDKs by using the command line interface (CLI) or by using a Java version checking tool. The CLI method involves typing the command "java -version" in the command prompt or terminal, while the Java version checking tool displays the installed Java versions in a user-friendly interface.

Why is it important to have multiple installed JDKs?

Having multiple installed JDKs allows for flexibility in running Java programs, as different programs may require different Java versions. It also ensures that you have access to the latest updates and features of Java, and can easily switch between versions as needed.

How often should I check the Java version on my installed JDKs?

It is recommended to check the Java version on your installed JDKs whenever you install a new JDK or update your current JDK. This ensures that you are always aware of the Java versions available on your system and can make necessary updates or changes as needed.

What should I do if I find an outdated Java version on my installed JDKs?

If you find an outdated Java version on your installed JDKs, you should update it to the latest version. This can be done by downloading the latest version from the official Java website and installing it on your system. It is also recommended to uninstall any older versions to avoid conflicts and ensure optimal performance.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
6
Views
4K
  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top