Java Shell Command getJavaVersion(String command)

Here you can find the source of getJavaVersion(String command)

Description

Retrieves the version of the java vm indicated by the command string.

License

Mozilla Public License

Parameter

Parameter Description
command a parameter

Exception

Parameter Description
IOException an exception
InterruptedException an exception

Declaration

public static String getJavaVersion(String command) throws IOException, InterruptedException 

Method Source Code


//package com.java2s;
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class Main {
    /**//  w w  w.j  a v a2 s  . c  o  m
     * Retrieves the version of the java vm indicated by the command string.
     * 
     * @param command
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    public static String getJavaVersion(String command) throws IOException, InterruptedException {
        String run_command = "'" + command + "' -version";
        System.out.println(run_command);
        Process p;
        String[] args = { command, "-version" };
        p = Runtime.getRuntime().exec(args);
        p.waitFor();
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        String line = reader.readLine();
        int row = 0;
        while (line != null) {
            line = reader.readLine();
            if (line.startsWith("Java(TM)")) {
                if (line.contains("build")) {
                    int start = line.indexOf("build");
                    System.out.println(start);
                    return line.substring(start + "build".length()).replace(")", "").trim();
                } else {
                    return null;
                }
            }
        }
        return null;
    }
}

Related

  1. runCommand(final ProcessBuilder command)
  2. runCommand(String cmd)
  3. runCommand(String cmd)
  4. runCommand(String command)