aspose file tools*
The moose likes Java in General and the fly likes help using runtime.exec Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "help using runtime.exec" Watch "help using runtime.exec" New topic

help using runtime.exec

Daniel Capelo

Joined: Feb 12, 2009
Posts: 5
im using runtime because i want to use cmd, but i have a problem:

when i want to change the directory to C:/users using the "cd" command it dont do anything, just open the cmd
can you guys help me

[Nitesh: Added code tags. Please UseCodeTags while posting code.]
Nitesh Kant

Joined: Feb 25, 2007
Posts: 1638

I think you need to read this classic article on the nuances of Runtime.exec()
Its a little old article and now its suggested to use ProcessBuilder but still you would have a lot to learn from that article.

In your case, i am not sure do you need to run "start" also. start will start a new dos prompt but all the other commands will still be executed in the process started using Runtime.exec(), so you would not see the result of your cd commands in the new dos prompt.

Moving to Intermediate forum.

apigee, a better way to API!
Daniel Capelo

Joined: Feb 12, 2009
Posts: 5
Ok thanxs ill try, i know is older but it is a good command , but if you can teach me processbuilder it can be great
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 39386
If you can work out what it says in Daconta, adding ProcessBuilder is quite simple.
Daniel Capelo

Joined: Feb 12, 2009
Posts: 5
i dont know why, but the command "cd" that change the directory on DOS, doesnt work in Runtime.exec, do you know why?.

the commands "dir","time","exit" work fine
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24187

Hi Daniel,

"The current directory" is a concept that applies to one specific executing process. When you use Runtime.exec() to execute a CMD.EXE, which in turn executes the "CD" command, that one specific executing process is the CMD.EXE shell. In other words, your code is working; "CD" is executed, and it changes the current directory of that CMD.EXE, which promptly exits.

Now, surely you've heard the old puzzle about the tree falling in the forest? This is like that. If you change the current directory of a process that then quits right away, does that effect anything? And the answer is no, of course.

Now, on to your next question: no, there's no other way to change the current directory of the executing JVM. It is what it is.

If you're trying to set the current directory for some other program that you're going to execute later, then you can create a batch file, and put the cd command and the command to run the other program into that batch file, and then Runtime.exec() the batch file.

[Jess in Action][AskingGoodQuestions]
Daniel Capelo

Joined: Feb 12, 2009
Posts: 5
thanxs, i fix it with processbuilder:

whit i can change the current directory.

but i have another question, using "processbuilder" can i use more than 2 arguments in it, like ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2", "myArg3", "myArg4",etc) ?
prasad guna
Ranch Hand

Joined: Feb 12, 2009
Posts: 43

i am also facing this problem but i use cmd for 'FFMPEG' in webapplication. i can't mention the path of 'cmd.exe'
how can solve it .. otherwise i mention my ffmpag.exe path directely to first argument... please provide right way..

I agree. Here's the link:
subject: help using runtime.exec