我刚接触Linux系统,似乎有太多的Java文件夹。
Java -version给我:
Java版本“1.7.0_55”
OpenJDK运行时环境(rhel-2.4.7.1。el6_5-x86_64 u55-b13)
OpenJDK 64位服务器虚拟机(build 24.51-b03,混合模式)
当我试图构建一个Maven项目时,我得到了错误:
Error: JAVA_HOME is not defined correctly.
We cannot execute /usr/java/jdk1.7.0_05/bin/java
请问root用户和非root用户需要修改哪些文件,java到底在哪里?
所有操作步骤(找到java,父目录,编辑文件,…)一个解决方案
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
结果:
# 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)让我们把完整的命令分解成几个部分
$(readlink -ze $(which java) | xargs -0 dirname | xargs -0 dirname)
2)从java命令中找到java路径
# $(which java)
"/usr/bin/java"
3)由符号路径得到相对路径
# readlink -ze /usr/bin/java
"/usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64/bin/java"
4)获取/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)获取“/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"