Guys,
How to check for java installation in windows, i want some code/pseudo code/algorithm..
Help me out.....

use java -help on command line to find various options which you may use to get information about JRE installation on windows.

Interesting, well on the command line one would type java -version but you want to execute code within your program to determine the runtime version. Well I think this should work:

Process java = Runtime.getRuntime().exec("cmd /C java -version");
BufferedReader in = new BufferedReader(new InputStreamReader(java.getInputStream()));
String line;
while ((line = in.readLine()) != null)
    System.out.println(line);

It should give you the following output from which you could extract the version:

java version "1.6.0_03"
Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode, sharing)

However, it doesn't work :icon_confused: Well at least you have something to work with... sorry mate.
Oh and the command I executed is a Windows based command that will not work on Linux.

Interesting, well on the command line one would type java -version but you want to execute code within your program to determine the runtime version. Well I think this should work:

Process java = Runtime.getRuntime().exec("cmd /C java -version");
BufferedReader in = new BufferedReader(new InputStreamReader(java.getInputStream()));
String line;
while ((line = in.readLine()) != null)
    System.out.println(line);

It should give you the following output from which you could extract the version:

java version "1.6.0_03"
Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode, sharing)

However, it doesn't work :icon_confused: Well at least you have something to work with... sorry mate.
Oh and the command I executed is a Windows based command that will not work on Linux.

Thank u dude...
Let me try...

Thank u dude...
Let me try...

I tried with the following program

 class test{
    public static void main(String args[]){

      Process java = Runtime.getRuntime().exec("cmd /C java -version");

      BufferedReader in = new BufferedReader(new InputStreamReader(java.getInputStream()));

      String line;

      while ((line = in.readLine()) != null)

      System.out.println(line);
}
}

saved as test.java, when i compiled this, it gives error "cannot read test.java"

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Test
{
	public static void main(String args[]) throws IOException
	{
		Process java = Runtime.getRuntime().exec("cmd /C java -version");
		BufferedReader in = new BufferedReader(new InputStreamReader(java.getInputStream()));
		String line;
		while ((line = in.readLine()) != null)
			System.out.println(line);
	}
}

Well there are a few imports that you must first have, though I've never seen such an error. What IDE are you using? On and Test is prefered over test for class file names :icon_wink: But for me this code has no output :icon_sad:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Test
{
	public static void main(String args[]) throws IOException
	{
		Process java = Runtime.getRuntime().exec("cmd /C java -version");
		BufferedReader in = new BufferedReader(new InputStreamReader(java.getInputStream()));
		String line;
		while ((line = in.readLine()) != null)
			System.out.println(line);
	}
}

Well there are a few imports that you must first have, though I've never seen such an error. What IDE are you using? On and Test is prefered over test for class file names :icon_wink: But for me this code has no output :icon_sad:

Dude, no out put :(...

hehe yeah that's what I said! but it was suppose to work. For example, instead of cmd /C java -version try using cmd /C dir and you'd get the output from the command prompt. Does anyone have any ideas?

Well, that is about the longest way around the problem if you simply want to know the java version from within a program. All you need is

System.getProperty("java.version");

There is a lot of other info available as well

for (java.util.Map.Entry s : System.getProperties().entrySet())
            System.out.println(s.getKey()+": "+s.getValue());

But all that is completely irrelevant if you are trying to check to see if java is installed on a machine. If Java isn't installed, Java code won't run. You will need a batch script or something else if you want to check for Java before running or installing an application.

The original poster does not explain the context, nor what the he/she needs to do with that information. Without a little more detail, you can't really provide a sufficient answer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.