Java Check Java Version on Multiple Installed JDKs

  • Thread starter Thread starter rathodnileshn
  • Start date Start date
  • Tags Tags
    Java
AI Thread Summary
The discussion centers on how to determine the version of multiple Java installations on a computer. It highlights that using the command prompt to execute "java -version" does not allow for output redirection to a file directly, but suggests using "java -version 2>filename" to capture the output, as it is sent to stderr. The conversation also touches on using System.getProperty("java.version") in Java code, which only returns the version of the currently running JVM, not the JAVA_HOME environment variable. Additionally, it emphasizes the importance of ensuring that the executable in the system PATH matches the version specified in JAVA_HOME to avoid confusion with tools like Ant. The utility "which" is recommended for identifying the Java executable path on Windows, confirming that the default Java executable may be located in "C:\WINDOWS\system32\java.exe".
rathodnileshn
Messages
2
Reaction score
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
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.
 
Hey,

Thank you very much!
You solved my problem!

Thanking you,
Nilesh Rathod.
 
Glad to be of help!
Come and visit us often!
 
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
1
Views
1K
Replies
1
Views
3K
Replies
1
Views
2K
Back
Top