?
Solved

Runtime.exec()

Posted on 2002-05-07
6
Medium Priority
?
364 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
5 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 400 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

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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 …
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

807 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