Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

start process from java when java run as service

I have a java process running as windows server using prcorun (http://commons.apache.org/proper/commons-daemon/); unfortunatly I have to launch an external legacy command written in C/C++.

both

Process myProcess = Runtime.getRuntime().exec(command);

and

Process myProcess = new ProcessBuilder(command, arg).start();

work well when java is launched as a stand-alone application, but when I start java as service it replies

command not found

also with

Process myProcess = Runtime.getRuntime().exec("dir");

command not found

I think is a problem due to windows services.

Any suggestion?

like image 442
venergiac Avatar asked Jul 15 '26 13:07

venergiac


2 Answers

I would try to do a quick test and print the PATH environment variable in your service. What I usually found when you run some command as a service, the PATH might not be totally available (which can also explain why DIR is not working for you). If that the case, when starting the service, you have to make sure the PATH include both the normal bin and your legacy bin.

like image 65
smurf Avatar answered Jul 18 '26 03:07

smurf


As the error says, the command is not found in the path. You'll need to set the environment variable PATH to the child process's environment. Look at exec(cmd, String[] env) method. You can create an array of environment variables (key value pairs) and pass it to exec().

like image 22
Bloodysock Avatar answered Jul 18 '26 03:07

Bloodysock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!