Linux 为什么 Maven 使用 JDK 1.6 但我的 java -version 是 1.7

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18813828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:50:51  来源:igfitidea点击:

Why Maven uses JDK 1.6 but my java -version is 1.7

javalinuxmacosmaven

提问by Ninja

I'm new to maven, and also to MacOS.

我是 maven 的新手,也是 MacOS 的新手。

I have setup maven in my terminal, and when getting the version settings (using mvn -v) it seems it uses JDK 1.6, while I have JDK 1.7 installed. Is there anything wrong?

我在我的终端中设置了 maven mvn -v,当我安装了 JDK 1.7 时,它似乎使用了 JDK 1.6的版本设置(使用)。有什么不对的吗?

The commands I enter are these:

我输入的命令是这些:

blues:helloworld Ninja$ java -version
java version "1.7.0_05"
Java(TM) SE Runtime Environment (build 1.7.0_05-b06)
Java HotSpot(TM) 64-Bit Server VM (build 23.1-b03, mixed mode)`
java version "1.7.0_05"
Java(TM) SE Runtime Environment (build 1.7.0_05-b06)
Java HotSpot(TM) 64-Bit Server VM (build 23.1-b03, mixed mode)`
blues:helloworld Ninja$ mvn -v
Apache Maven 3.1.0 (893ca28a1da9d5f51ac03827af98bb730128f9f2; 2013-06-28 10:15:32+0800)
Maven home: /usr/local/Cellar/maven/3.1.0/libexec
Java version: 1.6.0_51, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: zh_CN, platform encoding: EUC_CN
OS name: "mac os x", version: "10.8.4", arch: "x86_64", family: "mac"
Apache Maven 3.1.0 (893ca28a1da9d5f51ac03827af98bb730128f9f2; 2013-06-28 10:15:32+0800)
Maven home: /usr/local/Cellar/maven/3.1.0/libexec
Java version: 1.6.0_51, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: zh_CN, platform encoding: EUC_CN
OS name: "mac os x", version: "10.8.4", arch: "x86_64", family: "mac"

采纳答案by Gaurav Vaish

add the following to your ~/.mavenrc:

将以下内容添加到您的~/.mavenrc

export JAVA_HOME=/Library/Java/JavaVirtualMachines/{jdk-version}/Contents/Home

Second Solution:

第二种解决方案:

echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile

echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile

回答by redFIVE

You can also explicitly tell maven which java version to compile for. You can try adding the maven-compiler-pluginto your pom.

您还可以明确告诉 Maven 编译哪个 Java 版本。您可以尝试将其添加maven-compiler-plugin到您的 pom.xml 文件中。

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

If you imported a maven project into an IDE, then there is probably a maven setting in your IDE for default compiler that your maven runner is using.

如果您将 Maven 项目导入 IDE,那么您的 IDE 中可能有一个 Maven 设置,用于您的 Maven 运行器正在使用的默认编译器。

回答by Oskar

Get into

进入

/System/Library/Frameworks/JavaVM.framework/Versions

and update the CurrentJDKsymbolic link to point to

并更新CurrentJDK符号链接以指向

/Library/Java/JavaVirtualMachines/YOUR_JDK_VERSION/Contents/

E.g.

例如

cd /System/Library/Frameworks/JavaVM.framework/Versions
sudo rm CurrentJDK
sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/ CurrentJDK

Now it shall work immediately.

现在它将立即起作用。

回答by DEREK LEE

You can also do,

你也可以这样做,

<properties>
      ...  

      <!-- maven-compiler-plugin , aka JAVA Compiler 1.7 -->
      <maven.compiler.target>1.7</maven.compiler.target>
      <maven.compiler.source>1.7</maven.compiler.source>

      ...  
</properties>

回答by Yuri

I am late to this question, but I think the best way to handle JDK versions on MacOS is by using the script described at: http://www.jayway.com/2014/01/15/how-to-switch-jdk-version-on-mac-os-x-maverick/

我对这个问题迟到了,但我认为在 MacOS 上处理 JDK 版本的最佳方法是使用以下描述的脚本:http: //www.jayway.com/2014/01/15/how-to-switch-jdk -version-on-mac-os-x-maverick/

回答by Yonatan Abrego

For Eclipse Users. If you have a Run Configuration that does clean package for example.

对于 Eclipse 用户。例如,如果您有一个执行清理包的运行配置。

In the Run Configuration panel there is a JRE tab where you can specify against which runtime it should run. Note that this configuration overrides whatever is in the pom.xml.

在 Run Configuration 面板中有一个 JRE 选项卡,您可以在其中指定它应该针对哪个运行时运行。请注意,此配置会覆盖 pom.xml 中的任何内容。

回答by Vikas Singh

Please check the compatibility. I struggled with mvn 3.2.1and jdk 1.6.0_37for many hours. All variables were set but was not working. Finally I upgraded jdk to 1.8.0_60and mvn 3.3.3and that worked. Environment Variables as following:

请检查兼容性。我挣扎着mvn 3.2.1,并jdk 1.6.0_37为几个小时。所有变量都已设置但不起作用。最后我升级了jdk to 1.8.0_60mvn 3.3.3并且奏效了。环境变量如下:

JAVA_HOME=C:\ProgramFiles\Java\jdk1.8.0_60 
MVN_HOME=C:\ProgramFiles\apache-maven\apache-maven-3.3.3 
M2=%MVN_HOME%\bin extend system level Path- ;%M2%;%JAVA_HOME%\bin;

回答by Wei Qiu

@MasterGaurav 's solution is perfectly working,

@MasterGaurav 的解决方案非常有效,

I normally put the java switch thing into a zsh function:

我通常将 java switch 的东西放入 zsh 函数中:

alias java_ls='/usr/libexec/java_home -V 2>&1 | grep -E "\d.\d.\d[,_]" | cut -d , -f 1 | colrm 1 4 | grep -v Home'

function java_use() {
    export JAVA_HOME=$(/usr/libexec/java_home -v )
    echo export "JAVA_HOME=$(/usr/libexec/java_home -v )" > ~/.mavenrc
    export PATH=$JAVA_HOME/bin:$PATH
    java -version
}

Then you can run java_lstwo get all of the available jvm on your machine, run java_use 1.7to use the 1.7 version for both java and maven.

然后你可以运行java_ls两个获取你机器上所有可用的jvm,运行java_use 1.7以使用java和maven的1.7版本。

回答by Sergey Luchko

It helped me. Just add it in your pom.xml.

它帮助了我。只需将它添加到您的 pom.xml 中即可。

By default maven compiler plugin uses Java 1.5 or 1.6, you have to redefine it in your pom.xml.

默认情况下,maven 编译器插件使用 Java 1.5 或 1.6,您必须在 pom.xml 中重新定义它。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>