流程构建器不使用wmic打印控制台输出

时间:2017-01-10 13:12:48

标签: java command-line cmd

我正在尝试使用流程构建器打印chrome版本,当我在Windows命令提示符下直接执行wmic命令时,这可以正常使用命令提示符,同样不能使用流程构建器

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);
    }
}

预期

必须打印以下输出:Version = 55.0.2883.87

建议一些解决方案

1 个答案:

答案 0 :(得分:1)

尝试

try
{
    name1 = htmlDoc.DocumentNode.SelectSingleNode("//input[@name='name1']").GetAttributeValue("value", "");
} catch (Exception)
{
    name1 = "";
}

try
{
    name2 = htmlDoc.DocumentNode.SelectSingleNode("//input[@name='name2']").GetAttributeValue("value", "");
} catch (Exception)
{
    name2 = "";
}

try
{
    name3 = htmlDoc.DocumentNode.SelectSingleNode("//input[@name='name3']").GetAttributeValue("value", "");
} catch (Exception)
{
    name3 = "";
}

您需要引用的路径如下:

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

我认为即使您没有cmd也可以直接执行 "name='C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'"

编辑剪切整个代码(在wmic路径中也需要双斜杠):

wmic