Solved

Capturing the output of an OS command

Posted on 2006-11-12
5
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

630 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