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

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)

2
  • For better help sooner, post an SSCCE, that will supposedly explain what a StreamWrapper is. It would also help to ask a specific question, as Jon alluded to, even if it is only "How do I solve this exception?". Commented Apr 2, 2011 at 7:24
  • Runtime.exec starts a new Process for every invokation. exec("wmic") will start a process without arguments and exec(TEMP) has commandline arguments without an executable. The result is nonsense and fails. Commented Apr 2, 2011 at 8:43

1 Answer 1

1

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");
Sign up to request clarification or add additional context in comments.

2 Comments

Why not use a String[] for the args to rt.exec()? That way such output paths as C:\\Program Files\\InstallList.txt can be handled without escapes or obtuse substitution characters.
@Andrew - in general, true. In this particular case, it makes no difference. (The "\" has to be escaped anyway ...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.