• Post Reply Bookmark Topic Watch Topic
  • New Topic

Process command execution?  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello to all,

i have a package(named hello) with three classes in it.
I want to call one of the classes of the package throug a process command.

i try:

String[] cmd = {"cd", "/home/test" , "java hello.run_hello"}

prcs Runtime.getRuntime().axec(cmd);

Am i doing something wrong?
Can seme help?

Thank you in advance!
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am assuming "axec", along with others, are spelling errors... anyway....

String[] cmd = {"cd", "/home/test" , "java hello.run_hello"}


You are asking the process shell to do a "cd /home/test java hello.run_hello". First, the cd command doesn't take that many parameters. And second, a subshell is used for the process, so any change dir is lost upon return.

Henry
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Runtime.exec has several methods that take a path to execute the process in. Use that instead of the "cd /home/test".

Oh, and search for and read the article "When Runtime.exec() won't". It's a must-read when using Process.
 
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always handle that kind of sequence of commands - "change directory" then "execute program" by creating a batch file that gets exec()ed

This has the advantage that you can set environment values etc and make the batch file as large as you need.

Remember to consume the output streams as covered in the article Rob directed you to.

Bill
 
Nikkos Nikou
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the usefull help, and sorry for the spelling errors that occured.

N.N.
 
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use a ProcessBuilder, a newer class which basically has the same functionality as Runtime.exec(), but it's easier to use, I believe. If only because the directory() and environment() are clearly labeled. Most of the article Rob referenced still applies, however.

And I also agree with Bill - I often execute a shell script, rather than trying to cram everything into in single complex exec() command. But since the arrival of ProcessBuilder, I'm more willing to control the whole thing from Java.
 
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't forget to Google the article "Why Runtime.exec() won't" it's vital when using this!
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So vital it has been mentioned thrice now:
Rob Prime wrote:Oh, and search for and read the article "When Runtime.exec() won't". It's a must-read when using Process.

William Brogden wrote:Remember to consume the output streams as covered in the article Rob directed you to.

 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martijn speed reading FAIL
 
Nikkos Nikou
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the vital backup info.
The solution i came up with is: I first find the "/" , and then move to the desired folder where i want to execute the command.
I will also post the solution.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!