Reputation: 7717
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?
Upvotes: 3
Views: 849
Reputation: 1722
In my case I used
cmd /c <<YOUR COMMAND>>
eg. Process myProcess = Runtime.getRuntime().exec("cmd /c dir");
also I added the envinronments. as suggested by smurf
private static String[] getEnv() {
Map<String, String> env = System.getenv();
String[] envp = new String[env.size()];
int i = 0;
for (Map.Entry<String, String> e : env.entrySet()) {
envp[i++] = e.getKey() + "=" + e.getValue();
}
return envp;
}
...
Process myProcess = Runtime.getRuntime().exec("cmd /c dir",getEnv());
Alternative to java.lang.Runtime.exec() that can execute command lines as a single string?
Upvotes: 1
Reputation: 2898
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().
Upvotes: 1
Reputation: 49
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.
Upvotes: 1