Solved

Runtime.exec()

Posted on 2002-05-07
6
350 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
[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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

738 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