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)

genesis
  • 50,477
  • 20
  • 96
  • 125
Hemant
  • 1
  • 2
  • For better help sooner, post an [SSCCE](http://pscode.org/sscce.html), 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?". – Andrew Thompson Apr 02 '11 at 07: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. – josefx Apr 02 '11 at 08:43

1 Answers1

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");
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 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 Thompson Apr 02 '11 at 07:57
  • @Andrew - in general, true. In this particular case, it makes no difference. (The "\" has to be escaped anyway ...) – Stephen C Apr 02 '11 at 09:18