Maven 中的 Java_home [英] Java_home in Maven

查看:32
本文介绍了Maven 中的 Java_home的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行 mvn -version 时,我注意到 java_home 指向 ...jdkjre(如下所示).错了吗?难道它不应该指向 ...jdk.x.y.z (没有 jre)?如果是这样,我该如何重置?(在全局%java_home%指向jdk目录)

When I ran mvn -version, I noticed the java_home points to ...jdkjre (as shown below). Is that wrong? Isn't it supposed to point to ...jdk.x.y.z (without the jre)? If so, how do I reset it? (In global %java_home% points to the jdk directory)

C:UsersOwner>mvn -version
Apache Maven 2.2.1 (r801777; 2009-08-06 15:16:01-0400)
Java version: 1.7.0_17
Java home: C:Program FilesJavajdk1.7.0_17jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7" version: "6.1" arch: "amd64" Family: "windows"

推荐答案

不,没有错.它指向您的 JDK 使用的 JRE,这就是它应该做的.如果你在 maven 之外打印 JAVA_HOME,它应该打印正确:

No, it's not wrong. It is pointing to the JRE used by your JDK, which is what it's supposed to. If you print out JAVA_HOME outside maven, it should print correctly:

C:>echo %JAVA_HOME%
C:Program FilesJavajdk1.7.0_07

C:>mvn -version
Apache Maven 3.0.4 (r1232337; 2012-01-17 10:44:56+0200)
Maven home: C:APPSapache-maven-3.0.4in..
Java version: 1.7.0_07, vendor: Oracle Corporation
Java home: C:Program FilesJavajdk1.7.0_07jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
C:>

所以基本上JAVA_HOME需要指向JDK安装(maven需要tools.jar)但maven实际上使用JDK中的jre来运行自己.

So basically JAVA_HOME needs to point to a JDK installation (maven needs the tools.jar) but maven actually uses the jre within the JDK to run itself.

当使用mvn -version时,maven使用java内部java.home属性,从源代码:

When using mvn -version, maven uses java internal java.home property, as can be seen from source code:

version.append( "Java home: " + System.getProperty( "java.home", "<unknown java home>" ) ).append( LS );

此属性与 JAVA_HOME 环境设置不是一回事,因此它可能会欺骗您.它实际上是一个动态属性,向您显示哪个 JRE 正在运行您的代码.如果你编译并执行一个Test.java测试类打印出来,你可以看到如果你的JAVA_HOME指向一个JDK,java.home的值不等于你的JAVA_HOME.这是意料之中的.

This property is not the same thing as JAVA_HOME environment setting, so it might fool you. It is actually dynamic property showing you which JRE is running your code. If you compile and execute a Test.java test class printing the same, you can see that if your JAVA_HOME points to a JDK, the value of java.home is not equal to your JAVA_HOME. This is expected.

引用这个:

JAVA_HOME 和 java.home 有什么区别?

What's the difference between JAVA_HOME and java.home?

JAVA_HOME 是 JDK 安装目录,例如 C:jdk5.它的意思是设置为环境变量并在 Windows 批处理文件中引用或Unix 脚本.我总是在我的 Windows 控制面板和 .tcsh 中找到它文件,以及其他常见的环境变量.一些Java应用程序为此目的使用名称 jdk.home,我认为是一个更好的名字.但是JAVA_HOME从一开始就被使用,并且是现在是惯例.

JAVA_HOME is the JDK install directory, e.g., C:jdk5. It's meant to be set as an environment variable and referenced in Windows batch files or Unix scripts. I always have it in my Windows Control Panel and .tcsh files,along with other common environment variables. Some Java applications use the name jdk.home for this purpose, which I think is a better name. But JAVA_HOME has been used since the beginning and is now a convention.

java.home 是 JRE 安装目录,例如 C:jdk5jre,或C:Program FilesJavajre1.5.0_06.不像JAVA_HOME,我没见过java.home 作为环境变量.java.home 是一个内置的 Java系统属性,其值为 JRE 安装目录.由于所有Java 系统属性也作为 Ant 构建属性公开,您也可以在构建文件中使用 ${java.home}.

java.home is the JRE install directory, e.g., C:jdk5jre, or C:Program FilesJavajre1.5.0_06. Unlike JAVA_HOME, I never seen java.home as an environment variable. java.home is a build-in Java system property, whose value is the JRE install directory. Since all Java system properties are also exposed as Ant build properties, you can also use ${java.home} in build files.

jre.home 会是一个更好的名字吗?也许吧,但我认为 Sun 不会改变它.

Would jre.home be a better name? Maybe, but I don't think Sun will change it.

可以看到maven在mvn.bat上使用了JAVA_HOME:

You can see that maven uses JAVA_HOME on mvn.bat:

:endInit
SET MAVEN_JAVA_EXE="%JAVA_HOME%injava.exe"
..
%MAVEN_JAVA_EXE% %MAVEN_OPTS% -classpath %CLASSWORLDS_JAR% ..

如果你想确定,你可以在 mvn.bat 中注释掉@echo off"语句,这样你就可以看到它正在被使用.

And if you want to make sure, you can comment out "@echo off" statement in mvn.bat, so you can see that it is being used.

TL;DR:根据您提供的信息,您的配置是正确的,无需更改任何内容.

TL;DR: Based on the information you've given, your configuration is correct, no need to change anything.

多亏了这个帖子,还有一个关于这令人困惑的问题,这导致 Maven 版本 3.5.4 的输出发生变化.

thanks to this thread, there was also an issue about this being confusing, which resulted in change of output for Maven version 3.5.4.

这篇关于Maven 中的 Java_home的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆