如何在Linux中为所有用户设置JAVA_HOME

时间:2022-01-30 22:57:16

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

我是Linux系统的新手,似乎有太多的Java文件夹。

java -version gives me:

java - version给我:

  • java version "1.7.0_55"
  • java版本“1.7.0_55”
  • OpenJDK Runtime Environment (rhel-2.4.7.1.el6_5-x86_64 u55-b13)
  • OpenJDK(rhel-2.4.7.1运行时环境。el6_5-x86_64 u55-b13)
  • OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)
  • OpenJDK 64位服务器VM(构建24.51-b03,混合模式)

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

当我尝试构建Maven项目时,我得到了错误:

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?

你能告诉我需要为root用户和非root用户修改哪些文件吗? java在哪里?

14 个解决方案

#1


167  

  1. find /usr/lib/jvm/java-1.x.x-openjdk
  2. 找到/usr/lib/jvm/java - 1. - x.x openjdk
  3. vim /etc/profile

    vim / etc / profile

    Prepend sudo if logged in as not-privileged user, ie. sudo vim

    Prepend sudo如果登录为非特权用户,即。sudo vim

  4. Press 'i' to get in insert mode
  5. 按“i”进入插入模式
  6. add:

    添加:

    export JAVA_HOME="path that you found"
    
    export PATH=$JAVA_HOME/bin:$PATH
    
  7. logout and login again, reboot, or use source /etc/profile to apply changes immediately in your current shell
  8. 再次登录、重新启动或使用源/etc/profile立即在当前shell中应用更改

#2


56  

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

对于所有用户,我建议在/etc/profile中放置以下一行

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.

这将动态更新,并与替代系统很好地工作。但是请注意,更新将只在新的登录shell中进行。

#3


25  

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

您可以使用/etc/profile或更好的文件,比如/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.

您必须记住,这个文件只装载了新的登录shell。所以在bash -l或新的gnome-session之后,它不会随着新的Java版本而改变。

#4


16  

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:

在RHEL 7中,其他的答案都没有为我“坚持”,甚至在/etc/profile或~/中直接设置JAVA_HOME和PATH。bash_profile不会工作。每次我试图检查JAVA_HOME是否设置好时,它都会空出来:

$ echo $JAVA_HOME
    (<-- no output)

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

我要做的就是在/etc/profile.d/jdk_home中设置一个脚本。

#!/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.

我最初忽略了第一行(#!/bin/sh),没有它就不行。

Now it's working:

现在的工作:

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

#5


6  

It's Very easy to set path in Linux . Do as follows :

在Linux中设置路径很容易。做如下:

Step-1 Open terminal and type sudo gedit .bashrc

步骤1打开终端,输入sudo gedit .bashrc

Step-2 It will ask you your password . After typing password ,it will open the bash file . Then go to end and type below

第二步,它会问你密码。输入密码后,将打开bash文件。然后去结束,在下面的类型。

step-3

步骤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 from file

步骤4然后保存文件并退出文件

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

以上是针对单个用户的。对于所有用户,您必须遵循以下步骤

Step-1 gedit /etc/profile

步骤1中/ etc / profile

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

步骤2出口JAVA_HOME = / usr / lib / jvm / java-8-openjdk-amd64 /

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

步骤3出口路径= $路径:$ JAVA_HOME / bin

Hope this helps. Thanks!

希望这个有帮助。谢谢!

#6


5  

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

做Oracle所做的事情(作为Sun的前雇员,我不习惯)

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

在latestJavaRelease /usr/java/default中,latestJavaRelease是您想要使用的版本。

then export JAVA_HOME=/usr/java/default

然后出口JAVA_HOME = / usr / java /违约

#7


2  

Copy the bin file path you installed

复制您安装的bin文件路径

"YOUR PATH"

“你的路径”

open terminal and edit environment file by typing following command,

输入以下命令打开终端并编辑环境文件,

"sudo nano /etc/environment"

“sudo纳米/etc/environment”

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

在这个文件中,添加如下一行(用刚才复制的路径替换YOUR_PATH):

JAVA_HOME="YOUR_PATH"

JAVA_HOME = " YOUR_PATH "

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

这应该足以设置环境变量。现在重新加载这个文件:

"source /etc/environment"

“源/etc/environment”

now test it by executing:

现在通过执行测试:

echo $JAVA_HOME

echo $ JAVA_HOME

#8


2  

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

在Linux上,我将这一行添加到~/.profile:

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

#9


1  

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

在设置JAVA_HOME时,让我分享设置JAVA_HOME或任何其他环境变量的一些好处:

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.

1)很容易升级JDK,而不会影响到指向JAVA_HOME的应用程序启动和配置文件。您只需要下载新版本,并确保您的JAVA_HOME指向Java的新版本。这是使用环境变量或链接的最佳好处。

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

2) JAVA_HOME变量是简短的,而不是JDK安装目录的完整路径。

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.

3) JAVA_HOME变量是平*立性,即如果您的启动脚本使用JAVA_HOME,那么它可以在Windows和UNIX上运行,无需任何修改,您只需在各自的操作系统上设置JAVA_HOME。

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

阅读更多:http://javarevisited.blogspot.com/2012/02/how-to-set-javahome-environment-in.html # ixzz4BWmaYIjH

#10


1  

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

#11


1  

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):
  2. 不建议仅仅因为原因(如/etc/profile中所述)而更改/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.*
  • 除非你知道你在做什么,否则改变这个文件不是一个好主意。最好创建一个自定义。sh shell脚本在/etc/ profile。d对您的环境进行自定义更改,因为这将防止在将来的更新中需要合并
  1. So as stated above create /etc/profile.d/custom.sh file for custom changes.

    如上所述,创建/etc/ profile.d/custom。用于自定义更改的sh文件。

  2. Now, to always keep updated with newer versions of Java being installed, never put the absolute path, instead use:

    现在,为了始终保持更新版本的Java安装,不要使用绝对路径,而是使用:

#if making jdk as java home

如果将jdk作为java home。

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

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

OR

#if making jre as java home

如果将jre作为java home

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

导出JAVA_HOME=$(readlink -f /usr/bin/java | sed“s:/bin/java::”)

  1. And remember to have #! /bin/bash on the custom.sh file
  2. 记住要有#!/bin/bash定制。sh文件

#12


1  

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.

设置完变量后,请确保将适当的权限设置到安装它的java目录。

chmod -R 755 /usr/java

#13


0  

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

可能是一个好主意,以源任何你编辑的档案,以保存必须使用一个新的登录。

either: source /etc/ or . /etc/

要么:来源/etc/或。/ etc /

Where is whatever profile you edited.

哪里是你编辑的个人资料。

#14


-1  

I use the line:

我用直线:

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

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

我的~ /。配置文件,因此它在登录时使用默认java目录的基础。这是bash。

#1


167  

  1. find /usr/lib/jvm/java-1.x.x-openjdk
  2. 找到/usr/lib/jvm/java - 1. - x.x openjdk
  3. vim /etc/profile

    vim / etc / profile

    Prepend sudo if logged in as not-privileged user, ie. sudo vim

    Prepend sudo如果登录为非特权用户,即。sudo vim

  4. Press 'i' to get in insert mode
  5. 按“i”进入插入模式
  6. add:

    添加:

    export JAVA_HOME="path that you found"
    
    export PATH=$JAVA_HOME/bin:$PATH
    
  7. logout and login again, reboot, or use source /etc/profile to apply changes immediately in your current shell
  8. 再次登录、重新启动或使用源/etc/profile立即在当前shell中应用更改

#2


56  

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

对于所有用户,我建议在/etc/profile中放置以下一行

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.

这将动态更新,并与替代系统很好地工作。但是请注意,更新将只在新的登录shell中进行。

#3


25  

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

您可以使用/etc/profile或更好的文件,比如/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.

您必须记住,这个文件只装载了新的登录shell。所以在bash -l或新的gnome-session之后,它不会随着新的Java版本而改变。

#4


16  

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:

在RHEL 7中,其他的答案都没有为我“坚持”,甚至在/etc/profile或~/中直接设置JAVA_HOME和PATH。bash_profile不会工作。每次我试图检查JAVA_HOME是否设置好时,它都会空出来:

$ echo $JAVA_HOME
    (<-- no output)

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

我要做的就是在/etc/profile.d/jdk_home中设置一个脚本。

#!/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.

我最初忽略了第一行(#!/bin/sh),没有它就不行。

Now it's working:

现在的工作:

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

#5


6  

It's Very easy to set path in Linux . Do as follows :

在Linux中设置路径很容易。做如下:

Step-1 Open terminal and type sudo gedit .bashrc

步骤1打开终端,输入sudo gedit .bashrc

Step-2 It will ask you your password . After typing password ,it will open the bash file . Then go to end and type below

第二步,它会问你密码。输入密码后,将打开bash文件。然后去结束,在下面的类型。

step-3

步骤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 from file

步骤4然后保存文件并退出文件

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

以上是针对单个用户的。对于所有用户,您必须遵循以下步骤

Step-1 gedit /etc/profile

步骤1中/ etc / profile

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

步骤2出口JAVA_HOME = / usr / lib / jvm / java-8-openjdk-amd64 /

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

步骤3出口路径= $路径:$ JAVA_HOME / bin

Hope this helps. Thanks!

希望这个有帮助。谢谢!

#6


5  

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

做Oracle所做的事情(作为Sun的前雇员,我不习惯)

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

在latestJavaRelease /usr/java/default中,latestJavaRelease是您想要使用的版本。

then export JAVA_HOME=/usr/java/default

然后出口JAVA_HOME = / usr / java /违约

#7


2  

Copy the bin file path you installed

复制您安装的bin文件路径

"YOUR PATH"

“你的路径”

open terminal and edit environment file by typing following command,

输入以下命令打开终端并编辑环境文件,

"sudo nano /etc/environment"

“sudo纳米/etc/environment”

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

在这个文件中,添加如下一行(用刚才复制的路径替换YOUR_PATH):

JAVA_HOME="YOUR_PATH"

JAVA_HOME = " YOUR_PATH "

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

这应该足以设置环境变量。现在重新加载这个文件:

"source /etc/environment"

“源/etc/environment”

now test it by executing:

现在通过执行测试:

echo $JAVA_HOME

echo $ JAVA_HOME

#8


2  

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

在Linux上,我将这一行添加到~/.profile:

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

#9


1  

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

在设置JAVA_HOME时,让我分享设置JAVA_HOME或任何其他环境变量的一些好处:

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.

1)很容易升级JDK,而不会影响到指向JAVA_HOME的应用程序启动和配置文件。您只需要下载新版本,并确保您的JAVA_HOME指向Java的新版本。这是使用环境变量或链接的最佳好处。

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

2) JAVA_HOME变量是简短的,而不是JDK安装目录的完整路径。

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.

3) JAVA_HOME变量是平*立性,即如果您的启动脚本使用JAVA_HOME,那么它可以在Windows和UNIX上运行,无需任何修改,您只需在各自的操作系统上设置JAVA_HOME。

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

阅读更多:http://javarevisited.blogspot.com/2012/02/how-to-set-javahome-environment-in.html # ixzz4BWmaYIjH

#10


1  

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

#11


1  

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):
  2. 不建议仅仅因为原因(如/etc/profile中所述)而更改/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.*
  • 除非你知道你在做什么,否则改变这个文件不是一个好主意。最好创建一个自定义。sh shell脚本在/etc/ profile。d对您的环境进行自定义更改,因为这将防止在将来的更新中需要合并
  1. So as stated above create /etc/profile.d/custom.sh file for custom changes.

    如上所述,创建/etc/ profile.d/custom。用于自定义更改的sh文件。

  2. Now, to always keep updated with newer versions of Java being installed, never put the absolute path, instead use:

    现在,为了始终保持更新版本的Java安装,不要使用绝对路径,而是使用:

#if making jdk as java home

如果将jdk作为java home。

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

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

OR

#if making jre as java home

如果将jre作为java home

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

导出JAVA_HOME=$(readlink -f /usr/bin/java | sed“s:/bin/java::”)

  1. And remember to have #! /bin/bash on the custom.sh file
  2. 记住要有#!/bin/bash定制。sh文件

#12


1  

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.

设置完变量后,请确保将适当的权限设置到安装它的java目录。

chmod -R 755 /usr/java

#13


0  

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

可能是一个好主意,以源任何你编辑的档案,以保存必须使用一个新的登录。

either: source /etc/ or . /etc/

要么:来源/etc/或。/ etc /

Where is whatever profile you edited.

哪里是你编辑的个人资料。

#14


-1  

I use the line:

我用直线:

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

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

我的~ /。配置文件,因此它在登录时使用默认java目录的基础。这是bash。