Solved

Capturing the output of an OS command

Posted on 2006-11-12
5
214 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
Comment Utility
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
Comment Utility
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
Comment Utility
error=2 :: no such file
0
 
LVL 5

Expert Comment

by:gkishoreji
Comment Utility
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
Comment Utility
>>however u can try this.,

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now