I built a java program that runs the command “jps” and sees all JVMs and kills a particular JVM by extracting its id from the output of JPS command. It is working fine when I run it on the ubuntu terminal. But then I wrote a script in bash to ssh that machine from other machine and run this program there.
ssh $host "java -cp daemon.jar JVMname;"
Now here comes the problem.
Exception in thread "main" java.io.IOException: Cannot run program "jps": error=2, No such file or directory at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029) at runtime.daemon.halt.main(halt.java:19)
Caused by: java.io.IOException: error=2, No such file or directory at java.lang.UNIXProcess.forkAndExec(Native Method) at java.lang.UNIXProcess.<init>(UNIXProcess.java:135)
at java.lang.ProcessImpl.start(ProcessImpl.java:130)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1021)
If I go to the the machine myself and run this same command it works. I do not want to switch to exec solution.
Any ideas
Thanks
This isn’t a Java issue per se, it’s down to the difference in your environment in both cases. Specifically, when you run interactively, your
$PATHvariable contains the directory forjps, whereas in the latter case via SSH it doesn’t.Have a look at the
.bash_profileand.bashrcfiles on the remote machine – I suspect the path will be set in the.bash_profilefile, which isn’t executed for non-login shells (such as your SSH invocation that runs a single command). If you set the path correctly in.bashrc, then your current invocation should start working.(Note this assumes you’re using bash for a shell, though most other shells have a similar distinction between the login shell and non-login shell init files.)