problem with Runtime.getRuntime().exec() under Windows

S

S!mb@

hi all,

here is my code (for linux)

String command = "md toto";
Process child = Runtime.getRuntime().exec(command);

this code works perfectly, but this one under windows :

String command = "mkdir toto";
Process child = Runtime.getRuntime().exec(command);

generate a :
java.io.IOException: CreateProcess: mkdir toto error=2

Do you know what is the problem ?
I need to developp a software that works as good on Windows as on *n*x


Jerem
 
T

Thomas Fritsch

S!mb@ said:

Hi Jerem,
here is my code (for linux)

String command = "md toto";
Process child = Runtime.getRuntime().exec(command);

this code works perfectly, but this one under windows :

String command = "mkdir toto";
Process child = Runtime.getRuntime().exec(command);

I'm confused, because I thought it was just the other way round:
"mkdir" on Linux, "md" on Windows.
generate a :
java.io.IOException: CreateProcess: mkdir toto error=2

Do you know what is the problem ?
I need to developp a software that works as good on Windows as on *n*x

Anyway: I would suggest not to use Runtime.exec at all for creating a
directory, but to use
boolean created = (new File("toto")).mkdir();
if (!created)
// do some error handling
instead, which I think is more platform-neutral for this task.
See also http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html#mkdir()
 
S

S!mb@

Thomas said:
I'm confused, because I thought it was just the other way round:
"mkdir" on Linux, "md" on Windows.

oups, you're right.
But the creation of a directory was just an example. If I try with the
command "dir" on Windows, I have also an exception :
java.io.IOException: CreateProcess: dir error=2

the problem seems to be when java tries to create a separate process to
run the command.

Jerem
 
R

Roedy Green

String command = "md toto";
Process child = Runtime.getRuntime().exec(command);

this code works perfectly, but this one under windows :

String command = "mkdir toto";
Process child = Runtime.getRuntime().exec(command);

1. you don't need to resort to exec to create a directory. See
File.mkDir and File.mkDirs. See http://mindprod.com/jgloss/file.html
for an intro to the many things you can do in an platform independent
way with files.

2. If you use exec, many commands such as mkdir are "internal" handled
by the command shell. There is no mkdir.exe. Therefore you must spawn
a command shell and give it a command. See
http://mindprod.com/jgloss/exec.html for details.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top