【问题标题】:Fatal error compiling: invalid target release: 1.7致命错误编译:无效的目标版本:1.7
【发布时间】:2015-06-01 11:21:13
【问题描述】:

我在 Stackoverflow 上搜索了其他解决方案,但仍然遇到问题。我正在尝试使用 1.7 运行,但 Maven 使用的是 1.6。我现在有点没有想法。有人有其他建议吗?

pom.xml:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
  </plugin>
  <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>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.2</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <createDependencyReducedPom>false</createDependencyReducedPom>
          <artifactSet>
            <excludes>
              <exclude>org.hamcrest:*</exclude>
              <exclude>org.mockito:*</exclude>
              <exclude>org.objenesis:*</exclude>
            </excludes>
          </artifactSet>
          <filters>
            <filter>
              <artifact>*:*</artifact>
              <excludes>
                <exclude>META-INF/LICENSE</exclude>
                <exclude>META-INF/license</exclude>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
              </excludes>
            </filter>
          </filters>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <manifestEntries>
                <Main-Class>com.mycompany.app.App</Main-Class>
                <Build-Number>1</Build-Number>
              </manifestEntries>
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>

当我尝试mvn clean packagemvn clean install 时,我得到:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project my-appy: Fatal error compiling: invalid target release: 1.7 -> [Help 1]

mvn -version 的输出是:

mvn -version

Apache Maven 3.2.3 (33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4; 2014-08-11T13:58:10-07:00)
Maven home: /usr/local/Cellar/maven/3.2.3/libexec
Java version: 1.6.0_65, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x", version: "10.10.2", arch: "x86_64", family: "mac"

这是其他人的建议

echo $JAVA_HOME

/Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home

Intellij -> 首选项 -> 编译器 -> Java 编译器

$java -version
java version "1.7.0_55"
Java(TM) SE Runtime Environment (build 1.7.0_55-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.55-b03, mixed mode)

【问题讨论】:

  • 您在 pom 文件或项目设置中定义了 1.7 目标。显然你不能在 1.6 JDK 中使用它。将目标字节码更改为 1.6
  • @NeplatnyUdaj 根据你的建议,我得到:Error: JAVA_HOME is not defined correctly. We cannot execute /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/bin/java
  • 你显然安装了 1.7.0_55,而不是 40。

标签: java maven intellij-idea


【解决方案1】:

显然,您的 Maven 使用的 Java 是 Java 6,因此无法使用目标 1.7 进行编译。

您显然已经设置了 JAVA_HOME,但您需要确保您使用的是正确的 java 版本,尝试java -version 并确保它是 1.7+

通过使用jEnv 管理多个Java 环境的工具,我已经解决了我在OS X 上的多个Java 版本问题。你应该试一试,它可以通过 Homebrew 安装。

【讨论】:

  • 试试 jEnv。您可能想要激活导出插件,它会在您切换 java 运行时自动更新您的 JAVA_HOME
猜你喜欢
  • 2018-08-03
  • 2015-06-14
  • 2015-10-14
  • 2017-10-29
  • 2022-09-26
  • 2015-04-02
  • 2021-04-10
  • 2020-01-12
  • 2019-03-02
相关资源
最近更新 更多