java执行命令提示问题

时间:2011-04-02 06:42:07

标签: java command execute prompt

public class CmdExec {

     public static void main(String argv[]) {
            try {
                Runtime rt = Runtime.getRuntime();
                StreamWrapper error, output;
                String TEMP = "/output:C:\\InstallList.txt product get name,version";     
                System.out.println(TEMP);
                CmdExec rte = new CmdExec();
                Process proc = rt.exec("wmic");
                proc = rt.exec(TEMP);

                error = rte.getStreamWrapper(proc.getErrorStream(), "ERROR");
                output = rte.getStreamWrapper(proc.getInputStream(), "OUTPUT");
                int exitVal = 0;
                error.start();
                output.start();
                error.join(3000);
                output.join(3000);
                exitVal = proc.waitFor();
                System.out.println("Output: "+output.message+"\nError: "+error.message);



     } catch (IOException e) {
         e.printStackTrace();
     } catch (InterruptedException e) {
         e.printStackTrace();
        }
}
}

获得例外:

java.io.IOException:CreateProcess:\ output:C:\ InstallList.txt product get name,version error = 123     at java.lang.ProcessImpl.create(Native Method)     在java.lang.ProcessImpl。(未知来源)     at java.lang.ProcessImpl.start(Unknown Source)     在java.lang.ProcessBuilder.start(未知来源)     在java.lang.Runtime.exec(未知来源)     在java.lang.Runtime.exec(未知来源)     在java.lang.Runtime.exec(未知来源)     在CmdExec.main(CmdExec.java:20)

1 个答案:

答案 0 :(得分:1)

问题是您正在尝试将"/output:C:\\InstallList.txt product get name,version"作为命令执行,但这不起作用。 (事实上​​,对我来说这看起来像是胡说八道。)

我希望你应该像这样执行命令:

    rt.exec("wmic /output:C:\\InstallList.txt product get name,version");