Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

Capturing the output of an OS command

I'm using Runtime.exec to execute and capture OS command's output.

The proggy is simple:

import java.io.*;

public class RuntimeExec {

      public static void main(String[] args) {
            StringBuffer sb      = new StringBuffer();
            BufferedReader br =      null;
            
            System.out.println("***        OS: "+System.getProperty("os.name")+" "+System.getProperty("os.arch"));
            System.out.println("*** EXECUTING: "+args[0]);
            System.out.println();
            String line      = "";
            try      {
                  Runtime      rt = Runtime.getRuntime();
                  Process      proc = rt.exec(args[0]);
                  
                  br = new BufferedReader(new      InputStreamReader(proc.getInputStream()));
                  proc.waitFor();
                  while ((line = br.readLine()) != null) {
                        sb.append(line+"\n");
                  }
                  System.out.print(sb.toString());
            } catch      (Exception e) {
                  e.printStackTrace();
            } finally {
                  if (br != null) {
                        try      {
                              br.close();
                        } catch      (IOException e) {
                              e.printStackTrace();
                        }
                  }
            }
      }
}


but it does not work properly with all commands. For example "dir" and "ver" will produce an exceptio and I cannot understand.

java.io.IOException: CreateProcess: ver error=2
        at java.lang.ProcessImpl.create(Native Method)
        at java.lang.ProcessImpl.<init>(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 RuntimeExec.main(RuntimeExec.java:15)

Commands like "ipconfig" are executed and captured with no problems.

What's wrong with dir?!


0
harvk
Asked:
harvk
  • 2
  • 2
2 Solutions
 
CEHJCommented:
How are you invoking those non-running ones? You need to start

cmd.exe /c

Also look at

http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

as you need to treat streams on multiple threads
0
 
sciuriwareCommented:
The DIR command does not exist, it's in-built in cmd.exe
However, especially with DIR, there are enough reasons to explore the disk yourself in JAVA.
E.g.: an empty device gives an error and not just empty.
Anticipating on several kinds of behaviour is more difficult than using the File methods.
And, working with threads to catch standard output AND error output can be cumbersome.
;JOOP
0
 
sciuriwareCommented:
error=2 :: no such file
0
 
gkishorejiCommented:
as sciuriware said, there is no file like 'dir.exe'  so u cant execute it..

however u can try this.,

args[0] = cmd /c dir c:

and dont wait for the process to end.
0
 
CEHJCommented:
>>however u can try this.,

That's now the third time that's been mentioned ;-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now