Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help using runtime.exec

 
Daniel Capelo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Daniel Capelo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 50258
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can work out what it says in Daconta, adding ProcessBuilder is quite simple.
 
Daniel Capelo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Daniel Capelo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanxs, i fix it with processbuilder:



whit pb.directory 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
Posts: 46
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic