Install java on linux

I need to install java on a blank server.
I use these commands to install java

sudo tar -xvzf jdk-11.0.12_linux-x64_bin.tar.gz  
export JAVA_HOME=/home/[email protected]/jvm/jdk-11.0.12
echo $JAVA_HOME
export PATH=$PATH:$JAVA_HOME/bin
echo $PATH

When I type java -version, it shows me the version that I installed. It seems that I installed JVM successfully, but the problem is that everyday, the java disappears and I have to re-installed. Do you have any idea how it happens and what I can do to resolve this? I am pretty new to this system and really needs your help.

Thanks for any suggestion.


Solution 1:

You appear to be setting JAVA_HOME and PATH only in the current terminal session.

You should adjust your JAVA_HOME and PATH variable in either .bashrc or .bash_profile in your home directory. Then maybe logout and back in. That way the PATH will be adjusted every time you open a new terminal.

https://docs.oracle.com/cd/E19062-01/sun.mgmt.ctr36/819-5418/gaznb/index.html https://linuxize.com/post/bashrc-vs-bash-profile/

Solution 2:

With your above command you install java in your home directory. If the software is not explicitly required to be available to only this single user, I'd recommend to stick to the official filesystem hierarchy ( for example see here for a basic explanation The Linux Directory Structure, Explained ).

Since Oracle Java is a prime example for "proprietary software that doesn’t obey the standard file system hierarchy", it would best go to the /opt folder.

  • Create a new folder with sudo mkdir /opt/java
  • Move the .tar file there and extract it as sudo
  • Optionally: Install a symlink to the /usr directory, to run java without the full path sudo update-alternatives --install /usr/bin/java java PATH_TO_BINARY_HERE 100

Not quite sure what the JAVA_HOME variable is needed for. In Oracle's install manual it is not mentioned. If you do need this to be persistent, you'd have to add it to .bashrc or .bash_profile .