Solved

Capturing the output of an OS command

Posted on 2006-11-12
5
216 Views
Last Modified: 2010-03-31
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
Comment
Question by:harvk
  • 2
  • 2
5 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 30 total points
ID: 17925199
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
 
LVL 24

Accepted Solution

by:
sciuriware earned 70 total points
ID: 17925251
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
 
LVL 24

Expert Comment

by:sciuriware
ID: 17925254
error=2 :: no such file
0
 
LVL 5

Expert Comment

by:gkishoreji
ID: 17925486
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17926539
>>however u can try this.,

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

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question