Aravindharaj G
Aravindharaj G

Reputation: 417

Need to get another java.exe version in java

I am using following code to print the version of java.exe. (This java.exe is not the current running java.exe). I don't know why it,s not working.

    List commands = new ArrayList();
    commands.add("C:\\Program Files\\Java\\jdk1.7.0_79\\bin\\java.exe");
    commands.add("version");
    ProcessBuilder pb = new ProcessBuilder(commands);
    System.out.println("Running command");
    Process process = pb.start();
    int errCode = process.waitFor();
    System.out.println("command executed, any errors? " + (errCode == 0 ?  "No" : "Yes"));
    System.out.println("Output:\n" + output(process.getInputStream()));

Upvotes: 2

Views: 482

Answers (1)

Unknown
Unknown

Reputation: 2137

Try this

List<String> commands = new ArrayList<String>();
commands.add("C:\\Program Files\\Java7\\jdk1.7.0_79\\bin\\java.exe");
commands.add("-version");
ProcessBuilder pb = new ProcessBuilder(commands);
        // pb.directory(new File("C:\\Program Files\\Java7\\jdk1.7.0_79\\bin"));
Process p = pb.start();
int errCode = p.waitFor();
System.out.println(errCode);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String s = null;
while ((s = stdInput.readLine()) != null) {
            System.out.println(s);
        }

The output of java -version is sent to the error stream - reading from that stream should result in the proper output

OUTPUT:

java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

Upvotes: 4

Related Questions