How to set JAVA_HOME in Linux for all users

2024-10-09 09:10:00
admin
原创
225
摘要:问题描述: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 Runtime Environme...

问题描述:

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 Runtime Environment (rhel-2.4.7.1.el6_5-x86_64 u55-b13)

  • OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)

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?


解决方案 1:

  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

  1. Press 'i' to get in insert mode

  2. add:

export JAVA_HOME="path that you found"

export PATH=$JAVA_HOME/bin:$PATH
  1. logout and login again, reboot, or use source /etc/profile to apply changes immediately in your current shell

解决方案 2:

对于所有用户,我建议在/etc/profile.d/java_home.sh以下几行中创建一个文件

# Set JDK installation directory according to selected Java compiler

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

这将动态更新,并与备选系统配合良好。但请注意,更新只会在新的登录 shell 中进行。

解决方案 3:

您可以使用 /etc/profile 或者更好的文件,例如 /etc/profile.d/jdk_home.sh

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

您必须记住,此文件仅在新的登录 shell 中加载。因此,在 bash -l 或新的 gnome-session 之后,它不会随着新 Java 版本的出现而改变。

解决方案 4:

  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
  1. Then save the file and exit from file

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

  1. sudo gedit /etc/profile

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

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

解决方案 5:

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:

$ echo $JAVA_HOME
    (<-- no output)

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.

Now it's working:

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

解决方案 6:

Copy the bin file path you installed

YOUR PATH

open terminal and edit environment file by typing following command,

sudo nano /etc/environment

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

JAVA_HOME="YOUR_PATH"

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

source /etc/environment

now test it by executing:

echo $JAVA_HOME

解决方案 7:

首先,您需要确定您的 PC 上安装了哪种 Java 以及要使用哪种 Java。为此,请以 root 权限打开终端。

 sudo su

 ls /usr/lib/jvm/

现在它将列出可用的 Java 版本。选择列出的版本。

复制路径到那里。

现在打开.bashrc

  nano ~/.bashrc

将以下命令添加到末尾

 export JAVA_HOME="path that you copied"

  export PATH=$JAVA_HOME/bin:$PATH

然后按 Ctrl+S 和 Ctrl+X 保存文件并退出

现在运行以下命令:

  source ~/.bashrc

解决方案 8:

之前的帖子给出的答案是有效的。但是对于以下问题,没有一个答案是完整的:

  1. 不建议更改 /etc/profile,原因如下(如 /etc/profile 中所述):

  • 除非您知道自己在做什么,否则最好不要更改此文件。最好在 /etc/profile.d/ 中创建一个 custom.sh shell 脚本来对您的环境进行自定义更改,因为这将避免将来更新时需要合并。*

  1. 因此,如上所述,创建 /etc/profile.d/custom.sh 文件以进行自定义更改。

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

如果将 jdk 设为 java home

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

或者

如果将 jre 设为 java home

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

  1. 并记住在 custom.sh 文件上有#! /bin/bash

解决方案 9:

做 Oracle 所做的事情(作为前 Sun 员工,我无法习惯这一点)

ln -s latestJavaRelease /usr/java/default

其中 latestJavaRelease 是您要使用的版本

然后导出JAVA_HOME=/usr/java/default

解决方案 10:

1...使用快捷键Ctlr+ Alt+T打开终端

2...执行以下命令:

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

source /etc/source3...(推荐)重启虚拟机/电脑。如果不想重启电脑,可以使用

4...使用快捷键Ctlr++打开终端Alt`T`

5...已验证 JAVA_HOME 安装

echo $JAVA_HOME

一行代码来自flob,感谢他们

解决方案 11:

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

解决方案 12:

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.

chmod -R 755 /usr/java

解决方案 13:

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=\"${zJavaHomePath}\"" >> $zFileProfile
echo "export PATH=$PATH:$JAVA_HOME/bin" >> $zFileProfile

Result:

# 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

Explanation:

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

# $(which java)
"/usr/bin/java"

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"

解决方案 14:

Step 1 - check the current java version by "echo $JAVA_HOME"

Step 2 - vim /etc/profile

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 4 - Save and exit :wq

Step 5 - "source /etc/profile/", this would execute the change

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

解决方案 15:

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.

解决方案 16:

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

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

解决方案 17:

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变量简短简洁,而不是JDK安装目录的完整路径。

3)JAVA_HOME变量与平台无关,即如果您的启动脚本使用JAVA_HOME,那么它可以在Windows和UNIX上运行而无需任何修改,您只需要在相应的操作系统上设置JAVA_HOME。

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

解决方案 18:

使用 SDKMAN sdkman.io在您的 sdk 之间切换。

它为您设置JAVA_HOME。

解决方案 19:

使用 sudo打开kafka-run-class.sh进行写入

您可以在 kafka 文件夹中找到kafka-run-class.sh:kafka/bin/kafka-run-class.sh

检查这些行

enter image description here

修改 else 部分中的 JAVA 变量以指向 java/bin 中的 java 可执行文件。如JAVA="$JAVA_HOME/java"

解决方案 20:

在 /etc/profile 中,如果你打开它,你就会知道不建议在该文件上写入内容。相反,你可以制作一个命令脚本(假设是 test.sh),转到 /etc/profile.d 文件夹并将 test.sh 放在那里。每次你重新启动实例时,它都会被 /etc/profile 自动调用。

解决方案 21:

对于新手来说,使用 vim 可能有点困难。我们可以改用 gedit 文本编辑器。

  1. 查找 /usr/lib/jvm/java-1.xx-openjdk

  2. 输入“gedit /etc/profile”或使用“sudo gedit /etc/profile”(如果以非特权身份登录)

  3. 在行末添加以下内容:

export JAVA_HOME="您找到的路径"

导出 PATH=$JAVA_HOME/bin:$PATH

  1. 在当前 shell 中输入“source /etc/profile”以应用更改

解决方案 22:

export JAVA_HOME="/usr/lib/jvm/jdk-*"解决了这个问题

解决方案 23:

而且我也遇到了别人在其他脚本中导出 PATH 的问题,在我的例子中,.bashrc 中有一行 sdkman

export PATH=/home/ars/.sdkman/candidates/gradle/current/bin:/home/ars/.nvm/versions/node/v15.14.0/bin:/home/ars/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

所以当我跑步时echo $JAVA_HOME会有结果,但跑步时echo $PATH没有$JAVA_HOME/bin

解决方案 24:

我使用以下行:

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

到我的 ~/.profile,这样它在登录时就会使用默认 java 目录的基础。这是针对 bash 的。

解决方案 25:

如果不起作用请尝试这个:

apt install openjdk-8-jdk-headless
相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   1565  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1354  
  信创国产芯片作为信息技术创新的核心领域,对于推动国家自主可控生态建设具有至关重要的意义。在全球科技竞争日益激烈的背景下,实现信息技术的自主可控,摆脱对国外技术的依赖,已成为保障国家信息安全和产业可持续发展的关键。国产芯片作为信创产业的基石,其发展水平直接影响着整个信创生态的构建与完善。通过不断提升国产芯片的技术实力、产...
国产信创系统   21  
  信创生态建设旨在实现信息技术领域的自主创新和安全可控,涵盖了从硬件到软件的全产业链。随着数字化转型的加速,信创生态建设的重要性日益凸显,它不仅关乎国家的信息安全,更是推动产业升级和经济高质量发展的关键力量。然而,在推进信创生态建设的过程中,面临着诸多复杂且严峻的挑战,需要深入剖析并寻找切实可行的解决方案。技术创新难题技...
信创操作系统   27  
  信创产业作为国家信息技术创新发展的重要领域,对于保障国家信息安全、推动产业升级具有关键意义。而国产芯片作为信创产业的核心基石,其研发进展备受关注。在信创国产芯片的研发征程中,面临着诸多复杂且艰巨的难点,这些难点犹如一道道关卡,阻碍着国产芯片的快速发展。然而,科研人员和相关企业并未退缩,积极探索并提出了一系列切实可行的解...
国产化替代产品目录   28  
热门文章
项目管理软件有哪些?
云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用