安装 Java-JDK 后,如何正确设置 PATH、HOME、JAVA_HOME 变量以便使用 maven?

安装 Java-JDK 后,如何正确设置 PATH、HOME、JAVA_HOME 变量以便使用 maven?

我访问了https://help.ubuntu.com/community/EnvironmentVariables并了解如何设置环境变量和本地用户变量,并获得了相当清晰的了解。但我仍然无法理解如何为使用 Java 的 Maven 设置不同的系统级别和用户级别变量,如 PATH、JAVA_HOME、HOME。此外,Java 有不同的较新版本,带有 JDK、JRE 包,而旧版本似乎在许多情况下可以工作。因此,当安装了所有类型的 JAVA 的 JDK 和 JRE 版本时,它变得非常令人费解。在我的 ubuntu 机器上(在 VM 中),我无法将 Maven 与 Java 一起使用,因为当我运行包含命令的 bash 脚本时收到以下警告和错误#!/bin/bash mvn clean install

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.inject.internal.cglib.core.$ReflectUtils$1 (file:/usr/share/maven/lib/guice.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of com.google.inject.internal.cglib.core.$ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[INFO] Scanning for projects...
[INFO] 
[INFO] --------------< com.multichainjavaapi:MultiChainJavaAPI >---------------
[INFO] Building MultiChainJavaAPI 0.4.17-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MultiChainJavaAPI ---
[INFO] Deleting /home/kamran/Downloads/tmp/API Demo for Multichain/Multichain API Demo for Students/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MultiChainJavaAPI ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/kamran/Downloads/tmp/API Demo for Multichain/Multichain API Demo for Students/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MultiChainJavaAPI ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 89 source files to /home/kamran/Downloads/tmp/API Demo for Multichain/Multichain API Demo for Students/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/kamran/Downloads/tmp/API Demo for Multichain/Multichain API Demo for Students/src/main/java/multichain/MC/src/module-info.java:[8,1] modules are not supported in -source 8
  (use -source 9 or higher to enable modules)
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.925 s
[INFO] Finished at: 2023-01-27T21:11:15+05:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project MultiChainJavaAPI: Compilation failure
[ERROR] /home/kamran/Downloads/tmp/API Demo for Multichain/Multichain API Demo for Students/src/main/java/multichain/MC/src/module-info.java:[8,1] modules are not supported in -source 8
[ERROR]   (use -source 9 or higher to enable modules)
[ERROR] 
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

我的 ubuntu 上有 eclipse 和 netbeans IDE 以及以下软件,但是我无法加载项目 Java API,并出现上述错误:

sudo which java
/usr/bin/java

sudo echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/>:/snap/bin

sudo mvn -version
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.17, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-58-generic", arch: "amd64", family: "unix"

sudo echo $JAVA_HOME
/usr/lib/jvm/java-1.11.0-openjdk-amd64

sudo java -version
openjdk version "11.0.17" 2022-10-18
OpenJDK Runtime Environment (build 11.0.17+8-post-Ubuntu-1ubuntu220.04)
OpenJDK 64-Bit Server VM (build 11.0.17+8-post-Ubuntu-1ubuntu220.04, mixed mode, sharing)

sudo javac -version
javac 11.0.17

sudo gedit /etc/profile.d/19.0.2.sh

导出 J2SDKDIR=/usr/lib/jvm/java-19.0.2

导出 J2REDIR=/usr/lib/jvm/java-19.0.2

导出 PATH=$PATH

导出JAVA_HOME=/usr/lib/jvm/java-19.0.2

导出 DERBY_HOME=/usr/lib/jvm/java-19.0.2/db

我哪里做错了?

答案1

您的sudo echo $PATH输出表明 $JAVA_HOME/bin 未添加到 PATH。安装 JAVA_HOME 的正确命令顺序应为:

JAVA_HOME=/usr/lib/jvm/java-19.0.2-openjdk
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin
export PATH

相关内容