1

I am trying to run java -version on a remote linux machine using ssh in the following manner-

ssh ravi@192.168.3.90 "java -version"

I am getting bash: java: command not found. But java is installed on the remote machine and I have kept JAVA_HOME in both the file i.e. /etc/profile and ~/.bashrc. I can get the java -version on it while running directly on it.

Why? My objective is the get java version. What changes are required to get the version of installed java on remote linux machine?

ravi
  • 6,140
  • 18
  • 77
  • 154

5 Answers5

2

What does ssh ravi@192.168.3.90 "which java" show you. It seems that java is not found from PATH.

oikku
  • 542
  • 2
  • 7
  • showing nothing :( FYI, I manually untar the java packages and appended the following `JAVA_HOME` and `PATH` in `/etc/profile` and `.bashrc` file. `export JAVA_HOME=/opt/jdk1.6.0_24` `export PATH=$PATH:$JAVA_HOME/bin` – ravi Apr 18 '13 at 16:27
2

It is not about java. It is mostly about SSH. When you run command using SSH you actually connect to remote machine using specific environment. In your case using user ravi. I believe that this user does not have java in his PATH variable defined in his profile script (e.g. .bachrc).

Try to run ssh ravi@192.168.3.90 "echo $PATH" and see that java is not there.

Now the question is what do you want. If you want just to run java use absolute path. If you want to be able to run java using your command line add java to PATH for user account you are using.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • There is something strange. Let me tell you the environment details also. The machine where i am executing is a virtual machine installed on the machine which is having ip 192.168.3.90. Now as per your suggestion, I run `ssh ravi@192.168.3.90 "echo $PATH"` which is giving me the `PATH` of local machine. :( Strange but true – ravi Apr 18 '13 at 16:39
  • 1
    "echo $PATH" will print local but "echo \$PATH" will print remote PATH. – oikku Apr 18 '13 at 18:50
2

I experienced same issue during building jenkins pipeline for java application as shown below.

Jenkins-Machine-->Remote_server(Build server)

When I was trying to run "which java" cmd from jenkins machine to remote server, which build jar file for my application .

$ ssh -i id_rsa ubuntu@REMOTE_MACHINE 'which java'

it yields empty response.

When I ssh in remote machine and put following command

$ which java
/usr/lib/jvm/jdk-12.0.2/bin/java

Then I figured out jenkins server is seeking /usr/bin/java on remote server and I found that /usr/bin/java does not exists.

So I created symbolic link for /usr/bin/java using jdk folder on remote server

$ sudo ln -s /usr/lib/jvm/jdk-12.0.2/bin/java /usr/bin/java
Shree Prakash
  • 2,052
  • 2
  • 22
  • 33
1

You need the java command to be on the PATH on the remote machine. If it is not, you will need to run it using an absolute filepath.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

If it doesn't work after you hava set JAVA_HOME both in .bashrc and .profile or /etc/bash.bashrc. You can refer this answer How do I set $PATH such that ssh user@host command works?

In this answer,you actually set JAVA_HOME in .bashrc only,but in which postion,it's important. In my Ubuntu16.04,it does work!

Zhi Zheng
  • 59
  • 4