简体   繁体   中英

Java program runs but not in bash script

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 $PATH variable contains the directory for jps , whereas in the latter case via SSH it doesn't.

Have a look at the .bash_profile and .bashrc files on the remote machine - I suspect the path will be set in the .bash_profile file, 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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM