Java configuration in linux

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?

26 Answers 26

set environment variables as follows

Edit the system Path file /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.

It didn’t work for me and the previous version remained as default. I changed the order of the path to export PATH=$/bin:$ and the version got updated. I think left to right priority works here.

.bash_profile and .bashrc are specific to bash, whereas .profile is read by many shells in the absence of their own shell-specific config files. /etc/bash_profile (fallback /etc/profile) is read before the user’s .profile for system-wide configuration.

Also you would have to logout and login for the changes to take effect for /etc/profile but only close and open a new terminal if you change ~/.bashrc

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:

export JAVA_HOME=/usr/java/ export PATH=$:$/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.

perfect! thank you. NB: You might want to remove the default openJDK from your system as it appears first in the global path. So after the above script is loaded, you will only pic-up «javac» not java, because of the default openjk-jre. ~sudo apt-get remove openjdk-*~

Читайте также:  Установка zabbix linux debian

I have probelm with it, I tried every method to update java, but still time my version is jdk1.7 but I set up the path to jdk1.8

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.

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

Set JAVA_HOME as follows using syntax export JAVA_HOME= . 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 
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:

$ echo $JAVA_HOME $ echo $PATH 

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

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:

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:

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:

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

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

$ 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:

Append the following lines

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

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

sudo update-alternatives --config java 

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

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 

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.

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

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

Читайте также:  Linux kaspersky защита выключена

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 >~/.bashrc export JAVA_HOME=$(update-alternatives --query java | grep Value | cut -d" " -f2 | sed 's!\(\/.*\)jre\(.*\)!\1!g') export JDK_HOME=$ export JRE_HOME=$/jre/ EOF 

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

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

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

cat /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

When I use option 1. above after installing «openjdk-11-jdk-headless» I get JAVA_HOME and JDK_HOME both equal to «/usr/lib/jvm/java-11-openjdk-amd64/bin/java», which I assume it not correct. At least Maven/mvn says «The JAVA_HOME environment variable is not defined correctly. NB: JAVA_HOME should point to a JDK not a JRE» j

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 .

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

export JAVA_HOME=/usr/java/ export PATH=$:$/bin 

Now type java -version it should display what you set in .bashrc file.

Читайте также:  Install package in suse linux

This will persist over sessions as well.

enter image description here

enter image description here

  1. Update bashrc file to add JAVA_HOME sudo nano ~/.bashrc
  2. Add JAVA_HOME to bashrc file. export JAVA_HOME=/usr/java/
    export PATH=$:$/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

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.

Once I’ve installed the openjdk version of the Java Development Kit on an Ubuntu machine, I use this procedure to create a JAVA_HOME environment variable that doesn’t need to be changed after every version upgrade of the openjdk installation.

Firstly, I issue a command to discover the directory in which the java executables are located for this java installation.

echo $(readlink -e `which java` | xargs -0 dirname) 

If I’m happy with the output from that, everything else can be derived from it.

Configuring the JAVA_HOME and PATH environment variables

Rather than adding more and more configurations to the ~/.bashrc file I’ve found it a cleaner practice to create a separate small file that ~/.bashrc can «include» when it runs.

Let’s call that file ~/.java_env_vars (but you could name it whatever you wish).

Add an «include condition» to the ~/.bashrc file

Open ~/.bashrc in any text editor and these lines to the end of the file:

# include the java environment configuration file here (if it exists) if [ -f "$HOME/.java_env_vars" ]; then . $HOME/.java_env_vars fi 

Create the Java environment variable configuration file

Open any text editor, create the Java environment configuration file ~/.java_env_vars and add this content to it:

#1. set a java_bin variable to the directory containing the actual Java executables. java_bin=$(readlink -e `which java` | xargs -0 dirname) #2. append "$java_bin" to the PATH environment variable export PATH=$PATH:"$java_bin" #3. assign the directory of the current Java installation to the JAVA_HOME environment variable. export JAVA_HOME=$(dirname "$java_bin") 

NOTE: exporting the PATH and JAVA_HOME variables just ensures that they’re always available wherever they’re needed.

Source your ~/.bashrc file (so that the changes you made to it are reflected) using the following command:

Test the Java environment variable configuration

Open a new terminal console, and test the new Java environment variables by issuing this command:

echo "$PATH" && echo "$JAVA_HOME" 

Output should be two lines, something like this:

/home/user/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-11-openjdk-amd64/bin /usr/lib/jvm/java-11-openjdk-amd64 

Источник

Оцените статью
Adblock
detector