开发者

java execute command prompt problem

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();
              开发者_JAVA百科  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();
        }
}
}

getting Exception :

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


The problem is that you are trying to execute "/output:C:\\InstallList.txt product get name,version" as a command and that isn't working. (Indeed, it looks like nonsense to me.)

I expect that you should be executing the command like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜