Solved

Capturing the output of an OS command

Posted on 2006-11-12
5
218 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

735 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