[ubuntu] How to set Java environment path in Ubuntu

I just installed JDK in Ubuntu with sudo apt-get install openjdk-6-jdk command, after the installation where's the Java bin directory located? And how can I set the environment path for that directory? I have little experience with Ubuntu, can anyone give some advice or suggest any good website for reference?

This question is related to ubuntu environment-variables java

The answer is


set environment variables as follows

Edit the system Path file /etc/profile

sudo gedit /etc/profile

Add following lines in end

JAVA_HOME=/usr/lib/jvm/jdk1.7.0
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH

Then Log out and Log in ubuntu for setting up the paths...


Java is typically installed in /usr/java locate the version you have and then do the following:

Assuming you are using bash (if you are just starting off, i recommend bash over other shells) you can simply type in bash to start it.

Edit your ~/.bashrc file and add the paths as follows:

for eg. vi ~/.bashrc

insert following lines:

export JAVA_HOME=/usr/java/<your version of java>
export PATH=${PATH}:${JAVA_HOME}/bin

after you save the changes, exit and restart your bash or just type in bash to start a new shell

Type in export to ensure paths are right.

Type in java -version to ensure Java is accessible.


How to install java packages:

Install desired java version / versions using official ubuntu packages, which are managed using alternatives:
sudo apt install -y openjdk-8-jdk
or/and other version: sudo apt install -y openjdk-11-jdk

Above answers are correct only when you have only one version for all software on your machine, and you can skip using update-alternatives. So one can quickly hardcode it in .bashrc or some other place:
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
but it's not healthy, as later on you may change the version.

Correct way to set JAVA_HOME (and optionally JAVA_SDK, JAVA_JRE )

The correct way (and mandatory when you have more than one), is to detect what update-alternative is pointing to, and always use update-alternatives to switch active version.

Here are the suggestions for both: only specific unix account or for all accounts (machine level).

1. for a specific unix account only:

Use this if you don't have permissions to do it at machine level.

cat <<'EOF' >>~/.bashrc

export JAVA_HOME=$(update-alternatives --query java | grep Value | cut -d" " -f2 | sed 's!\(\/.*\)jre\(.*\)!\1!g')
export JDK_HOME=${JAVA_HOME}
export JRE_HOME=${JDK_HOME}/jre/

EOF

2. To do it at machine level, and for all bourne shells, you need 2 steps:

2.a

cat <<'EOF' | sudo tee /etc/profile.d/java_home_env.sh >/dev/null

export JAVA_HOME=$(update-alternatives --query java | grep Value | cut -d" " -f2 | sed 's!\(\/.*\)jre\(.*\)!\1!g')
export JDK_HOME=${JAVA_HOME}
export JRE_HOME=${JDK_HOME}/jre/

EOF

As your shell might not be set as interactive by default, you may want to do this also:
2.b

cat <<'EOF' | sudo tee -a /etc/bash.bashrc >/dev/null
if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi
EOF

PS: There should be no need to update the $PATH, as update-alternatives takes care of the link to /usr/bin/.
More on: https://manpages.ubuntu.com/manpages/trusty/man8/update-alternatives.8.html


Ubuntu installs openjdk6 to /usr/lib/jvm/java-6-openjdk path. So you will have the bin in /usr/lib/jvm/java-6-openjdk/bin. Usually the classpath is automatically set for the java & related executables.


All you have to do now is to set the “JAVA_HOME” and “PATH” environment variables and then you are done. Enter the following commands to set your environment variables. Make sure that your environment variables point to a valid installation of JDK on your machine. For Ubuntu 18.04, the path is /usr/lib/jvm/java-8-openjdk-amd64/

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64

To check whether your JAVA_HOME path has been successfully saved, enter the following command to check.

echo $JAVA_HOME

Create/Open ~/.bashrc file $vim ~/.bashrc Add JAVA_HOME and PATH as refering to your JDK path

export JAVA_HOME=/usr/java/<your version of java>
export PATH=${PATH}:${JAVA_HOME}/bin

Save file

Now type java - version it should display what you set in .bashrc file. This will persisted over session as wel.

Example :

enter image description here

enter image description here


To Set JAVA_HOME / PATH for a single user, Login to your account and open .bash_profile file

$ vi ~/.bash_profile

Set JAVA_HOME as follows using syntax export JAVA_HOME=<path-to-java>. If your path is set to /usr/java/jdk1.5.0_07/bin/java, set it as follows:

export JAVA_HOME=/usr/java/jdk1.5.0_07/bin/java

Set PATH as follows:

export PATH=$PATH:/usr/java/jdk1.5.0_07/bin

Feel free to replace /usr/java/jdk1.5.0_07 as per your setup. Save and close the file. Just logout and login back to see new changes. Alternatively, type the following command to activate the new path settings immediately:

$ source ~/.bash_profile

OR

$ . ~/.bash_profile

Verify new settings:

$ echo $JAVA_HOME
$ echo $PATH

Tip: Use the following command to find out exact path to which java executable under UNIX / Linux:

$ which java

Please note that the file ~/.bashrc is similar, with the exception that ~/.bash_profile runs only for Bash login shells and .bashrc runs for every new Bash shell.

To Set JAVA_HOME / PATH for all user, You need to setup global config in /etc/profile OR /etc/bash.bashrc file for all users:

# vi /etc/profile

Next setup PATH / JAVA_PATH variables as follows:

export PATH=$PATH:/usr/java/jdk1.5.0_07/bin
export PATH=$PATH:/usr/java/jdk1.5.0_07/bin

Save and close the file. Once again you need to type the following command to activate the path settings immediately:

# source /etc/profile

OR

# . /etc/profile

You can install the default Ubuntu(17.10) java from apt:

sudo apt install openjdk-8-jdk-headless 

And it will set the PATH for you, if instead you need to install specific version of Java you can follow this YouTube


Set java version from the list of installed. For see the list of the installed version run following command:

update-java-alternatives --list

Then set your java version according to the following command:

sudo update-java-alternatives --set /usr/lib/jvm/java-1.8.0-openjdk-amd64

open jdk once installed resides generally in your /usr/lib/java-6-openjdk As usual you would need to set the JAVA_HOME, calsspath and Path In ubuntu 11.04 there is a environment file available in /etc where you need to set all the three paths . And then you would need to restart your system for the changes to take effect..

Here is a site to help you around http://aliolci.blogspot.com/2011/05/ubuntu-1104-set-new-environment.html


It should put java in your path, probably in /usr/bin/java. The easiest way to find it is to open a term and type "which java".


Step1:

sudo gedit ~/.bash_profile

Step2:

JAVA_HOME=/home/user/tool/jdk-8u201-linux-x64/jdk1.8.0_201
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH

Step3:

source ~/.bash_profile

if you have intalled only openJDK, the you should update your links, because you can have some OpenJDK intallation.

sudo update-alternatives --config java

after this

$gedit ~/.bashrc

add the following line in the file

JAVA_HOME=/usr/lib/jvm/YOUR_JAVA_VERSION export PATH=$PATH:$JAVA_HOME/bin export JAVA_HOME

you can get you java version with

java -version

To set up system wide scope you need to use the

/etc/environment file sudo gedit /etc/environment

is the location where you can define any environment variable. It can be visible in the whole system scope. After variable is defined system need to be restarted.

EXAMPLE :

sudo gedit /etc/environment

Add like following :

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
JAVA_HOME="/opt/jdk1.6.0_45/"

Here is the site you can find more : http://peesquare.com/blogs/environment-variable-setup-on-ubuntu/


  1. Update bashrc file to add JAVA_HOME

    sudo nano ~/.bashrc

  2. Add JAVA_HOME to bashrc file.

    export JAVA_HOME=/usr/java/<your version of java>
    export PATH=${PATH}:${JAVA_HOME}/bin

  3. Ensure Java is accessible

    java -version

  4. In Case of Manual installation of JDK, If you got an error as shown below

    Error occurred during initialization of VM
    java/lang/NoClassDefFoundError: java/lang/Object
    
  5. Execute the following command in your JAVA_HOME/lib directory:

    unpack200 -r -v -l "" tools.pack tools.jar

  6. Execute the following commands in your JAVA_HOME/jre/lib

    ../../bin/unpack200 rt.pack rt.jar ../../bin/unpack200 jsse.pack jsse.rar ../../bin/unpack200 charsets.pack charsets.jar

  7. Ensure Java is accessible

    java -version


Installation of Oracle Java:

  1. Donwload the tarball(.tar file) from Oracle website
  2. unzip it by sudo tar -xvpzf fileName -C /installation_folder_name
  3. change the files permission and ownership
  4. add the following two lines in /etc/profile

export JAVA_HOME=/home/abu/Java/jdk1.8.0_45/ export PATH=$JAVA_HOME/bin:$PATH

  1. restart the machine and check by java -version and javac -version

Open file /etc/environment with a text editor Add the line JAVA_HOME="[path to your java]" Save and close then run source /etc/environment


  • Open terminal (Ctrl+Alt+t)
  • Type

    sudo gedit .bashrc 
    
  • Enter password of ubuntu user
  • Go to last line of the file
  • Type below code in new line

    export JAVA_HOME=enter_java_path_here
    export PATH=$JAVA_HOME/bin:$PATH
    eg: export JAVA_HOME=/home/pranav/jdk1.8.0_131
        export PATH=$JAVA_HOME/bin:$PATH
    
  • Save the file
  • Type

    source ~/.bashrc
    

    in terminal

  • Done

You need to set the $JAVA_HOME variable

In my case while setting up Maven, I had to set it up to where JDK is installed.

First find out where JAVA is installed:

$ whereis java

java: /usr/bin/java /usr/share/java /usr/share/man/man1/java.1.gz

Now dig deeper-

$ ls -l /usr/bin/java

lrwxrwxrwx 1 root root 46 Aug 25 2018 /etc/alternatives/java -> /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java Dig deeper:

$ ls -l /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

-rwxr-xr-x 1 root root 6464 Mar 14 18:28 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

As it is not being referenced to any other directory, we'll use this.

Open /etc/environment using nano

$ sudo nano /etc/environment

Append the following lines

JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64

export JAVA_HOME

Reload PATH using

$. /etc/environment

Now,

$ echo $JAVA_HOME

Here is your output:

/usr/lib/jvm/java-1.8.0-openjdk-amd64

Sources I referred to:

https://askubuntu.com/a/175519

https://stackoverflow.com/a/23427862/6297483


I have a Linux Lite 3.8 (It bases on Ubuntu 16.04 LTS) and a path change in the following file (with root privileges) with restart has helped.

/etc/profile.d/jdk.sh

Let me Simplify , first download JDK from Oracle Website : Link

2] Then Extract it

3] Create a folder (jvm) in /usr/lib/ i.e /usr/lib/jvm

4] move the extracted folder from the jdk to /usr/lib/jvm/

*Note : use terminal , sudo , mv command i.e. sudo mv

5] Create a .sh file at /etc/profile.d/ eg: /etc/profile.d/myenvvar.sh

6] In the .sh file type

export JAVA_HOME=/usr/lib/jvm/jdk1.7.0

export PATH=$PATH:$JAVA_HOME/bin

*Note : use terminal , gedit and sudo eg: sudo gedit myenvvar.sh

7] Turn Off the Computer, after all these steps and Restart it

8]Open Terminal , and type

java -version

9] Check the output , then type

echo $JAVA_HOME

10] Check the output and be happy :)


Examples related to ubuntu

grep's at sign caught as whitespace "E: Unable to locate package python-pip" on Ubuntu 18.04 How to Install pip for python 3.7 on Ubuntu 18? "Repository does not have a release file" error ping: google.com: Temporary failure in name resolution How to install JDK 11 under Ubuntu? How to upgrade Python version to 3.7? Issue in installing php7.2-mcrypt Install Qt on Ubuntu Failed to start mongod.service: Unit mongod.service not found

Examples related to environment-variables

Using Environment Variables with Vue.js Adding an .env file to React Project Is there any way to set environment variables in Visual Studio Code? Test process.env with Jest How to set environment variables in PyCharm? ARG or ENV, which one to use in this case? how to set ASPNETCORE_ENVIRONMENT to be considered for publishing an asp.net core application? What is a good practice to check if an environmental variable exists or not? Passing bash variable to jq Tensorflow set CUDA_VISIBLE_DEVICES within jupyter

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log