263

我在 Netbeans 中编写了一些 Maven 代码,大约有 2000 多行。当我在 Netbeans 上编译它时,一切都很好,但如果我想在命令行上运行它,我会收到以下错误:

generics are not supported in -source 1.3
(use -source 5 or higher to enable generics)
        ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();

generics are not supported in -source 1.3
(use -source 5 or higher to enable generics)
        HashSet<Double> resid_List = new HashSet<Double>(Arrays.asList(resid_val));

generics are not supported in -source 1.3
(use -source 5 or higher to enable generics)
        List<Integer> ind_ovlpList = new ArrayList<Integer>(Arrays.asList(ind_ovlp));

generics are not supported in -source 1.3
(use -source 5 or higher to enable generics)
public class ColumnComparator implements Comparator<double[]> {

annotations are not supported in -source 1.3
(use -source 5 or higher to enable annotations)
@Override

我尝试使用Java 1.3.1,编译器错误,但出现了更多错误。我从其他帖子中发现我应该修改pom.xml,但我不知道如何。这是我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany</groupId>
  <artifactId>mavenmain</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>mavenmain</name>
    <url>http://maven.apache.org</url>

   <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

   <dependencies>
     <dependency>
       <groupId>junit</groupId>
        <artifactId>junit</artifactId>
       <version>3.8.1</version>
       <scope>test</scope>
     </dependency>
     <dependency>
        <groupId>gov.nist.math</groupId>
        <artifactId>jama</artifactId>
        <version>1.0.2</version>
     </dependency>
   </dependencies>
 </project>

如果你能帮助我,那就太好了!

4

5 回答 5

372

maven-compiler-plugin它已经存在于 pom.xml 中的插件层次结构依赖项中。签入有效的 POM。

简而言之,您可以使用如下属性:

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

我正在使用 Maven 3.2.5。

于 2015-07-28T11:54:32.763 回答
313
<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>(whatever version is current)</version>
        <configuration>
          <!-- or whatever version you use -->
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

请参阅 Maven 编译器插件的配置页面:

http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html

哦,还有:不要使用 Java 1.3.x,当前版本是 Java 11 或 17。

于 2013-05-23T20:44:20.720 回答
19

通常,您不想只重视source版本(javac -source 1.8例如),而是想同时重视版本sourcetarget版本(javac -source 1.8 -target 1.8例如)。
请注意,从 Java 9 开始,您可以同时传达信息并以更健壮的方式实现交叉编译兼容性 ( javac -release 9)。
包装命令的 Mavenjavac提供了多种方式来传达所有这些 JVM 标准选项。

如何指定JDK版本?

使用maven-compiler-pluginor maven.compiler.source/maven.compiler.target属性来指定 thesource和 thetarget是等价的。

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

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

根据编译器插件的 Maven 文档,它们是等效的, 因为编译器配置中的<source><target>元素使用属性maven.compiler.source并且maven.compiler.target如果它们被定义。

来源

Java 编译器的-source参数。
默认值为:1.6
用户属性是:maven.compiler.source

目标

Java 编译器的-target参数。
默认值为:1.6
用户属性是:maven.compiler.target

关于 和 的默认值sourcetarget请注意, 自从3.8.0maven 编译器以来,默认值已从 更改1.51.6

<release>maven-compiler-plugintag — 在3.6中指定 Java 版本的新方法

您可以使用release参数

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <release>9</release>
    </configuration>
</plugin>

您也可以只声明用户属性maven.compiler.release

<properties>
    <maven.compiler.release>9</maven.compiler.release>
</properties>

但是此时最后一个还不够,因为maven-compiler-plugin您使用的默认版本不依赖于足够新的版本。

Mavenrelease参数传递 release给 Java 编译器以访问新添加到 Java 9的JVM 标准选项, JEP 247: Compile for Older Platform Versions

针对特定 VM 版本的公共、受支持和记录的 API 进行编译。

source这种方式提供了一种为、targetbootstrapJVM 选项指定相同版本的标准方式。
请注意,指定bootstrap是交叉编译的好习惯,如果您不进行交叉编译也不会受到伤害。

指定 JDK 版本的最佳方法是什么?

Java 8 及以下

maven.compiler.source/maven.compiler.target属性使用两者都不maven-compiler-plugin是更好。它没有改变任何事实,因为最终这两种方式依赖于相同的属性和相同的机制:maven 核心编译器插件。

好吧,如果您不需要在编译器插件中指定除 Java 版本之外的其他属性或行为,那么使用这种方式更有意义,因为这样更简洁:

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

Java 9 及更高版本

论点(第三release点)是一种强烈考虑是否要对 和 使用相同版本source的方法target

于 2018-08-30T19:11:07.013 回答
11

我在eclipse neon simple maven java项目中遇到了同样的问题

但是我在 pom.xml 文件中添加了以下详细信息

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

右键单击项目> Maven>更新项目后(选中强制更新)

它解决了我在项目上显示错误

希望它会有所帮助

坦斯克

于 2017-05-03T07:19:26.357 回答
0
<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <fork>true</fork>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin> 
于 2019-04-23T05:35:42.930 回答