简体   繁体   中英

process builder doesn't print the console output using wmic

I was trying to print the version of chrome using process builder, this works fine with the command prompt when I executed wmic command directly in the windows command prompt, the same doesn't work with the process builder

String path= "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe";
    String[] cmd = { "CMD", "/C", "wmic datafile where name="+path+" get Version /value" };
    ProcessBuilder probuilder = new ProcessBuilder(cmd);
    Thread.sleep(5000);
    Process p = probuilder.start();
    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String readline;
    int i = 0;
    while ((readline = reader.readLine()) != null) {
        System.out.println(++i + " " + readline);
    }
}

Expected

It has to print the following output: Version=55.0.2883.87

Suggest some solution

try with

 String[] cmd = { "CMD", "/C", "wmic datafile where \"name='"+path+"'\" get Version /value" };

you need the path quoted like:

 "name='C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'"

I think even you can execute directly the wmic without the cmd.

edit whole code snipped(double slashes are also needed in the wmic path):

Runtime rt = Runtime.getRuntime();
String path= "C:\\\\Program Files (x86)\\\\Google\\\\Chrome\\\\Application\\\\chrome.exe";



Process p2=Runtime.getRuntime().exec("cmd /C wmic datafile where 'name=\""+path+"\"'  get Version ");


BufferedReader reader = new BufferedReader(new InputStreamReader(p2.getInputStream()));
BufferedReader stdError = new BufferedReader(new 
         InputStreamReader(p2.getErrorStream()));

String readline;
System.out.println("Output:\n");
while ((readline = reader.readLine()) != null) {
    System.out.println(readline);
}

System.out.println("Errors:\n");
while ((readline = stdError.readLine()) != null) {
    System.err.println(readline);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM