当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Compilation error during mvn -install

问题描述:

I've tried both mvn install and mvn clean install And both of them throw out the same error:

[Error] *dir/class.java* try-with-resources is not supported in -source 1.5

Here is the information of my environment:

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)

Maven home: /opt/maven

Java version: 1.8.0_77-debug, vendor: Oracle Corporation

Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.77-0.b03.el6_7.x86_64-debug/jre

Default locale: en_US, platform encoding: UTF-8

OS name: "linux", version: "2.6.32-431.23.3.el6.x86_64", arch: "amd64", family: "unix"

Is it because that the maven run on java 1.5 but not 1.8? If so, is there method to change the version that maven used?

网友答案:

If you use Java features which are only available from a specific Java version you should declare it in your pom.xml

<properties>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    ...
</properties>

The appropriate JDK should be in the PATH when you run mvn.

e.g.

JDK_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.77-0.b03.el6_7.x86_64-debug/
export PATH=${JDK_HOME}/bin:${PATH}
mvn <your_maven_parameters>
网友答案:

Change java version to 1.7 or higher

You can override version in pom

        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

In spring-boot project you can override version in properties

<properties>
    <java.version>1.8</java.version>
</properties>
网友答案:

try-with-resources is only available from java 1.7. For that you need to use source java version to >=1.7 and target version >= 1.7 (i.e. >= $source.version)

<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>
网友答案:

This has nothing to do with Linux. The only problem that you have is that you are using try-with-resource which was introduced by Java 7 and by default maven is configured to compile java source code with 1.5 spec. If you need to use try-with-resource feature, as @UUID specified, you can configure maven-compiler-plugin to compile using at source and target 1.7

Is it because that the maven run on java 1.5 but not 1.8?

No, Maven can run on any Java version (I don't know which version of Java that Maven doesn't run). However I think the maximum version that Maven can compile depends on the Java version that the Maven is running on. I.e. if your maven runs on JDK 7, then the maximum version of source and target would be Java 7 as well.

网友答案:

Your java -version returned a correct Java 8 version, Java version: 1.8.0_77-debug, vendor: Oracle Corporation Make sure that you have javac -version also returning correctly Java version: 1.8.0_77-debug, vendor: Oracle Corporation.

分享给朋友:
您可能感兴趣的文章:
随机阅读: