0

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

Aleks G
  • 56,435
  • 29
  • 168
  • 265
shujaat
  • 279
  • 6
  • 17
  • 2
    This is a duplicate to this question (interactive shells): http://stackoverflow.com/questions/940533/how-do-i-set-path-such-that-ssh-userhost-command-works – nwinkler Mar 08 '12 at 12:04
  • 1
    Can you post the content of the remote server's `~/.bashrc` file? – nwinkler Mar 09 '12 at 07:08

1 Answers1

2

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.)

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
  • Thanks for the reply. .bashrc is set but I dont have .bash_profile set in there. I am not sure it will do anything. But you pointed correctly. I ran this -bash-3.2$ ssh compute-0-8 "pwd" /export/home2/shujaat.hussain -bash-3.2$ ssh compute-0-8 "jps" bash: jps: command not found So jps did not run but when sshed it runs fine. What is missing? – shujaat Mar 08 '12 at 12:13
  • 2
    Take a look at the question I linked, it explains which file is executed from a non-interactive shell (ssh): http://stackoverflow.com/questions/940533/how-do-i-set-path-such-that-ssh-userhost-command-works – nwinkler Mar 08 '12 at 12:17
  • Thanks @nwinkler. I am still struggling a little although I am figured out the problem by the link you gave. I echoed the path noniteractively and the $PATH was not the same as I would work on the interactive terminal. The normal SSH reveals the path to be much longer. Any idea how the whole path could be exported. The problem is "jps" cant run if whole path is not exported which right now is only happening when we ssh conventionally. Thanks – shujaat Mar 09 '12 at 03:20