Define java home linux

How to set JAVA_HOME in Linux for all users

When I am trying to build a Maven project , I am getting error:

Error: JAVA_HOME is not defined correctly. We cannot execute /usr/java/jdk1.7.0_05/bin/java 

Could you please tell me which files I need to modify for root as well as not-root user and where exactly is java located?

25 Answers 25

  1. find /usr/lib/jvm/java-1.x.x-openjdk
  2. vim /etc/profile Prepend sudo if logged in as not-privileged user, ie. sudo vim
  3. Press ‘i’ to get in insert mode
  4. add:
export JAVA_HOME="path that you found" export PATH=$JAVA_HOME/bin:$PATH 

don’t forget to delete the double quotes and recreate them from your keyboard, because only copying and pasting may create troubles.

I think it is safe to mention how to save the updates you make by going into command mode by Pressing «Esc» and then typing «:w» and pressing «Enter».

For all users, I would recommend creating a file in /etc/profile.d/java_home.sh the following lines

# Set JDK installation directory according to selected Java compiler export JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:/bin/javac::") 

This will update dynamically and works well with the alternatives system. Do note though that the update will only take place in a new login shell.

to comply with just plain JRE (even headless) use export JAVA_HOME=$(readlink -f /usr/bin/java | sed «s:/bin/java::») instead — notice that I use just java , not javac

Upvote for readlink and update/upgrade compatible solution, even this is an 5 years old thread. I only recommend not to edit /etc/profile, but place your export inside custom file, e.g. /etc/profile.d/java_environment.sh , maybe you have to chmod +x java_environment.sh and reboot.

Perfect. Better than my clunky dirname -ing solution. If you’re like me and wanting to understand what’s going on here, this is a simple replacement of matching text from input with an empty string. The «default» character for replacements is the / , but as long as you’re consistent, you can replace the / with anything. In this case it’s colons as we use / for path separators.

You could use /etc/profile or better a file like /etc/profile.d/jdk_home.sh

export JAVA_HOME=/usr/java/jdk1.7.0_05/ 

You have to remember that this file is only loaded with new login shells.. So after bash -l or a new gnome-session and that it doesn’t change with new Java versions.

at least in my linux (raspbian), /etc/profile will source /etc/profile.d/*.sh so your file needs to be called jdk_home.sh so it gets sourced

/etc/profile.d/jdk_home.sh is the much more clean answer, at least for Ubuntu , /etc/profile is clumped with so much logic already. Didn’t seems wise to add more onto it.

None of the other answers were «sticking» for me in RHEL 7, even setting JAVA_HOME and PATH directly in /etc/profile or ~/.bash_profile would not work. Each time I tried to check if JAVA_HOME was set, it would come up blank:

Читайте также:  One time password linux

What I had to do was set up a script in /etc/profile.d/jdk_home.sh :

#!/bin/sh export JAVA_HOME=/opt/ibm/java-x86_64-60/ export PATH=$JAVA_HOME/bin:$PATH 

I initially neglected the first line (the #!/bin/sh ), and it won’t work without it.

$ echo $JAVA_HOME /opt/ibm/java-x86_64-60/ 

I had the same experience on RHEL 7. I removed the exports from the ~/.bash_profile and used this approach.

#!/bin/sh is not required in your jdk_home.sh. once you done the configuration make sure to logout and login again

Also, using the default jvm symlink for java_home is probably good stackoverflow.com/questions/663658/…

  1. Open terminal and type sudo gedit .bashrc
  2. It will ask you your password. After typing the password, it will open the bash file. Then go to end and type:
export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/" export PATH=$PATH:$JAVA_HOME/bin 

Above is for a single user. For all users, you have to follow below steps

  1. gedit /etc/profile
  2. export JAVA_HOME=»/usr/lib/jvm/java-8-openjdk-amd64/»
  3. export PATH=$PATH:$JAVA_HOME/bin

Copy the bin file path you installed

open terminal and edit environment file by typing following command,

In this file, add the following line (replacing YOUR_PATH by the just copied path):

That should be enough to set the environment variable. Now reload this file:

The answer is given previous posts is valid. But not one answer is complete with respect to:

  1. Changing the /etc/profile is not recommended simply because of the reason (as stated in /etc/profile):
  • It’s NOT a good idea to change this file unless you know what you are doing. It’s much better to create a custom.sh shell script in /etc/profile.d/ to make custom changes to your environment, as this will prevent the need for merging in future updates.*
  1. So as stated above create /etc/profile.d/custom.sh file for custom changes.
  2. Now, to always keep updated with newer versions of Java being installed, never put the absolute path, instead use:

#if making jdk as java home

export JAVA_HOME=$(readlink -f /usr/bin/javac | sed «s:/bin/javac::»)

OR

#if making jre as java home

export JAVA_HOME=$(readlink -f /usr/bin/java | sed «s:/bin/java::»)

Doing what Oracle does (as a former Sun Employee I can’t get used to that one)

ln -s latestJavaRelease /usr/java/default
Where latestJavaRelease is the version that you want to use

then export JAVA_HOME=/usr/java/default

First you need to find out which Java is installed in your PC and which one to use. For that open terminal with root permission.

Now it will list the available java versions. Select the listed version.

add the following commands to the end

 export JAVA_HOME="path that you copied" export PATH=$JAVA_HOME/bin:$PATH 

after that save the file and exit by pressing Ctrl+S followed by Ctrl+X

Now run the below command:

1. Using the short cut Ctlr + Alt + T to open terminal

2. Execute the below command:

echo export JAVA_HOME='$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")' | sudo tee /etc/profile.d/jdk_home.sh > /dev/null 

3. (Recommended) Restart your VM / computer. You can use source /etc/source if don’t want to restart computer

Читайте также:  Связь android и linux

4. Using the short cut Ctlr + Alt + T to open terminal

5. Verified JAVA_HOME installment with

One-liner copy from flob, credit to them

This is a very simple script to solve the problem

export JAVA_HOME_BIN=`which java` export JAVA_HOME_DIR=`dirname $JAVA_HOME_BIN` export JAVA_HOME=`dirname $JAVA_HOME_DIR` 

Posting as answer, as I don’t have the privilege to comment.

Point to note: follow the accepted answer posted by «That Dave Guy».

After setting the variables, make sure you set the appropriate permissions to the java directory where it’s installed.

All operational steps(finding java, parent dir, editing file. ) one solution

zFileProfile="/etc/profile" zJavaHomePath=$(readlink -ze $(which java) | xargs -0 dirname | xargs -0 dirname) echo $zJavaHomePath echo "export JAVA_HOME=\"$\"" >> $zFileProfile echo "export PATH=\$PATH:\$JAVA_HOME/bin" >> $zFileProfile 
# tail -2 $zFileProfile export JAVA_HOME="/usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64" export PATH=$PATH:$JAVA_HOME/bin 

1) Let’s break the full command into pieces

$(readlink -ze $(which java) | xargs -0 dirname | xargs -0 dirname) 

2) Find java path from java command

3) Get relative path from symbolic path

# readlink -ze /usr/bin/java "/usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64/bin/java" 

4) Get parent path of /usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64/bin/java

# readlink -ze /usr/bin/java | xargs -0 dirname "/usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64/bin" 

5) Get parent path of /usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64/bin/

# readlink -ze /usr/bin/java | xargs -0 dirname | xargs -0 dirname "/usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64" 

Step 1 — check the current java version by «echo $JAVA_HOME»

Step 3 — At the end of file you will find export JAVA_HOME, we need to provide the new path here, make sure that it is not relative.

Step 5 — «source /etc/profile/», this would execute the change

Step 6 — Again do a echo $JAVA_HOME — change would have been reflected.

Probably a good idea to source whatever profile you edit to save having to use a fresh login.

either: source /etc/ or . /etc/

Where is whatever profile you edited.

On Linux I add this line to my ~/.profile:

export JAVA_HOME=$(readlink -ze /usr/bin/javac | xargs -0 dirname -z | xargs -0 dirname) 

It seems a potentially correct answer but could you explain why it works, ie what it does and what the OP’s problem is? Also, you say «on Linux», but there are many different Linux distro’s and it might not work for all of them, please add for which distro this works.

While we are up to setting JAVA_HOME, let me share some benefits of setting JAVA_HOME or any other environment variable:

1) It’s easy to upgrade JDK without affecting your application startup and config file which points to JAVA_HOME. you just need to download new version and make sure your JAVA_HOME points to new version of Java. This is best benefit of using environment variable or links.

2) JAVA_HOME variable is short and concise instead of full path to JDK installation directory.

3) JAVA_HOME variable is platform independence i.e. if your startup script uses JAVA_HOME then it can run on Windows and UNIX without any modification, you just need to set JAVA_HOME on respective operating system.

Источник

How to Set JAVA_HOME Path in Ubuntu 20.04 and Ubuntu 22.04

JAVA_HOME Path

Many Java-based programs require the Java Runtime Environment (JRE) to run smoothly regardless of the operating system. For development, most IDEs like Eclipse and NetBeans require the Java Development Kit (JDK) on your computer. Whether you’re a newbie who wants to learn how to develop or an application you have installed requires a Java installation on your system, we’ve got you covered. Setting up the JAVA_HOME path is quite simple.

Читайте также:  Linux группа пользователя сервер

In this tutorial, we will learn how to install the JDK on Ubuntu 20.04 and Ubuntu 22.04 and set up the Java_HOME path. Let’s start with the installation of the JDK.

Install OpenJDK on Ubuntu

Note: Make sure that you have updated the Advanced Package Tool (APT) before you move forward to install OpenJDK.

Press Ctrl + Alt + T to open the terminal and enter the following command mentioned in the box. You can either install OpenJDK 8 or the newer versions OpenJDK 11 to OpenJDK 18. Not all versions are available on any Ubuntu version, you can check for available versions with the command:

Install OpenJDK 8

sudo apt install openjdk-8-jdk

Install OpenJDK 11

sudo apt install openjdk-11-jdk

Install OpenJDK 14

sudo apt install openjdk-14-jdk

Install OpenJDK 16

sudo apt install openjdk-16-jdk

Install OpenJDK 17

sudo apt install openjdk-17-jdk

Install OpenJDK 18

sudo apt install openjdk-18-jdk

You will be prompted to enter your sudo password to continue with the installation.

Install OpenJDK on Ubuntu Linux

Once you’ve entered that, wait for the system to finish the installation and then move on to step 2.

Set JAVA_HOME Path

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.

The value stored in the JAVA_HOME variable will be displayed on the terminal as you can see in the screenshot

Ubuntu JAVA_HOME Path

Add JAVA bin directory to the PATH variable

Like we have added JAVA_HOME path, we will now update the PATH variable as well. To do that, enter the following command on the terminal.

export PATH=$PATH:$JAVA_HOME/bin

This will append the java bin directory to the existing PATH variable. You can also check the PATH variable by entering the following command

Ubuntu Java Binary Path

Test JAVA setup

You have successfully installed OpenJDK on your machine. You can verify your installation by entering the following command on your terminal.

Check Java version

Through this simple tutorial, you have now configured the JAVA_HOME variable in your operating system. You can now easily run java based applications as well as development environments on your machine.

About This Site

Vitux.com aims to become a Linux compendium with lots of unique and up to date tutorials.

Latest Tutorials

Источник

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