How to install JAVA_HOME on Linux for all users

I am new to Linux system and there seem to be too many Java folders.

java -version gives me:

  • java version "1.7.0_55"
  • OpenJDK workspace (rhel-2.4.7.1.el6_5-x86_64 u55-b13)
  • OpenJDK 64-bit server version (build 24.51-b03, mixed mode)

When I try to create a Maven project, I get an error:

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

Could you tell me which files I need to change for both root and non-root users and where exactly is java?

+141
java linux java-home path-variables
Jul 08 '14 at
source share
17 answers
  1. find/usr/lib/jvm/java-1.xx-openjdk
  2. vim/etc/profile

    Add predo if you are logged in as an unprivileged user, i.e. sudo vim

  3. Press "I" to enter insert mode
  4. add:

     export JAVA_HOME="path that you found" export PATH=$JAVA_HOME/bin:$PATH 
  5. Log out and log back in, restart your computer, or use the source/etc/profile file source/etc/profile to immediately apply the changes to your current shell
+287
Jul 08 '14 at 9:07
source share

For all users, I would recommend placing the following line in /etc/profile

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

This will dynamically update and work well with an alternative system. Please note that updates will only occur in the new login shell.

+92
Apr 14 '15 at 8:26
source share

You can use / etc / profile or better a file, for example /etc/profile.d/jdk_home.sh

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

You must remember that this file is only loaded with new login shells. So, after bash -l or a new gnome session and that it does not change with new versions of Java.

+34
Jul 08 '14 at 9:07
source share

None of the other answers were β€œsticking” to me in RHEL 7, even setting JAVA_HOME and PATH directly in /etc/profile or ~/.bash_profile would not help. Every time I tried to check if JAVA_HOME was set, it would look empty:

 $ echo $JAVA_HOME (<-- no output) 

I needed to install the 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 

At first I ignored the first line ( #!/bin/sh ) and it would not work without it.

Now it works:

 $ echo $JAVA_HOME /opt/ibm/java-x86_64-60/ 
+27
Dec 08 '15 at 1:06 on
source share

It is very easy to set the path on Linux. Do the following:

Step 1 Open a terminal and enter sudo gedit .bashrc

Step-2 He will ask you for your password. After entering the password, it will open the bash file. Then go to the end and enter below

step 3

  export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/" export PATH=$PATH:$JAVA_HOME/bin 

Step 4 Then save the file and exit the file

Above for one user. For all users you must follow these steps

Step 1 gedit /etc/profile

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

Step 3 export PATH=$PATH:$JAVA_HOME/bin

Hope this helps. Thank!

+9
Sep 25 '16 at 19:44
source share

Doing what Oracle does (as a former Sun employee, I can't get used to it)

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

then export JAVA_HOME = / usr / java / default

+6
Oct 31 '14 at 16:46
source share

Reply to previous messages is valid. But not one answer is completed regarding:

  • Changing / etc / profile is not recommended simply because of a reason (as stated in / etc / profile):
  • It is not recommended to change this file if you do not know what you are doing. It is much better to create a custom.sh script wrapper in /etc/profile.d/ to make custom changes to your environment, as this will prevent the need for merging in future updates. *
  1. As stated above, create the file /etc/profile.d/custom.sh for custom changes.

  2. Now, to constantly update with new versions of Java that have been installed, never set the absolute path, use:

#if creating jdk as java home

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

OR

#if creating jre as java home

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

  1. And remember that #! / Bin / bash in the custom.sh file
+5
Sep 15 '17 at 5:14
source share

Copy the path to the bin file that you installed

 YOUR PATH 

open a terminal and edit the environment file by entering the following command,

 sudo nano /etc/environment 

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

 JAVA_HOME="YOUR_PATH" 

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

 source /etc/environment 

Now verify this by doing:

 echo $JAVA_HOME 
+4
Aug 03 '15 at 11:42 on
source share

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

 export JAVA_HOME=$(readlink -ze /usr/bin/javac | xargs -0 dirname -z | xargs -0 dirname) 
+2
Nov 04 '15 at 11:49
source share

It is probably a good idea to create any profile that you are editing to keep the need to use a new login.

either: source / etc / or, / etc. /

Where is which profile you edited.

+1
Jun 09 '15 at 0:35
source share

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

1) It is easy to update the JDK without affecting your application launch and the configuration file that points to JAVA_HOME. you just need to download the new version and make sure your JAVA_HOME points to the new version of Java. It is best to use environment variables or links.

2) The JAVA_HOME variable is short and short, not the full path to the JDK installation directory.

3) the JAVA_HOME variable is platform independence, i.e. if your script run uses JAVA_HOME, then it can run on Windows and UNIX without any changes, you just need to install JAVA_HOME on the appropriate operating system.

More details: http://javarevisited.blogspot.com/2012/02/how-to-set-javahome-environment-in.html#ixzz4BWmaYIjH

+1
Jun 14 '16 at 5:51 on
source share

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` 

And for testing:

 echo $JAVA_HOME 
+1
Apr 13 '17 at 15:43 on
source share

Posting as an answer, as I have no right to comment.

Note Note : Follow the accepted answer posted by "That Dave Guy."

After setting the variables, make sure that you set the appropriate permissions in the java directory where it was installed.

 chmod -R 755 /usr/java 
+1
Sep 18 '17 at 8:50
source share

Step 1 - check your current Java version for "echo $ JAVA_HOME"

Step 2 - vim / etc / profile

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

Step 4 - Save and Exit: wq

Step 5 - "source / etc / profile /", this will make the change

Step 6 - Echo $ JAVA_HOME Again - The change will be reflected.

+1
Oct. 31 '18 at 11:52
source share

Just add this command to your dockerfile

 RUN echo "JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")" | tee -a /etc/profile && source /etc/profile && echo $JAVA_HOME 
+1
May 16 '19 at 10:08
source share

1 ... Using the keyboard shortcut Ctlr + Alt + T to open a terminal

2 ... Run the following 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) Reboot the virtual machine / computer. You can use source /etc/source if you do not want to restart the computer

4 ... Using the keyboard shortcut Ctlr + Alt + T to open a terminal

5 ... Proven installments of JAVA_HOME with

 echo $JAVA_HOME 



One line copy with flob , credit to them

0
Sep 27 '19 at 9:03
source share

I use the line:

 export JAVA_HOME=$(readlink -f $(dirname $(readlink -f $(which java) ))/../) 

in my ~ / .profile, so it uses the default java directory base at login time. This is for bash.

-one
Jul 15 '15 at 1:21
source share



All Articles