Solved

Runtime.exec()

Posted on 2002-05-07
6
341 Views
Last Modified: 2010-03-31
Process p = Runtime.getRuntime().exec("dir");

Why does the above sentence throw the following error ?

java.io.IOException: CreateProcess: dir error=2

How do we use Runtime.getRuntime().exec() to execute DOS commands ?

Please help
0
Comment
Question by:sekharj
  • 3
6 Comments
 
LVL 3

Expert Comment

by:daitt
ID: 6993768
because dir is a DOS command, you need a command intepreter to run it.
Usually, you run it with "command /c dir"
0
 

Author Comment

by:sekharj
ID: 6993848
daitt,

Process p = Runtime.getRuntime().exec("command /c dir");
System.out.println(p.exitValue());

Those are the lines I used. I still get the error :

java.io.IOException: CreateProcess: command /c dir error=2
0
 
LVL 3

Accepted Solution

by:
daitt earned 100 total points
ID: 6994089
yeah the file is command.com .. it's somewhere in your system. THIs is an example that works on my system:

Process p = Runtime.getRuntime().exec("c:\\windows\\system32\\command.com /c dir");
p.waitFor();
System.out.println(p.exitValue());
0
 
LVL 3

Expert Comment

by:daitt
ID: 7015395
Does it answer your question, sekhari?
0
 
LVL 35

Expert Comment

by:girionis
ID: 8709879
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to daitt

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

912 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

17 Experts available now in Live!

Get 1:1 Help Now