263

I wrote some Maven code in Netbeans that has approximately more than 2000 lines. When I compile it on Netbeans, everything is fine, but if I want to run it on command line, I will get these errors:

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

I tried to use Java 1.3.1, compiler errors, but I got more errors. I found from other posts that I should modify pom.xml, but I do not know how. Here is my 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>

It would be great if you can help me!

Lii
  • 11,553
  • 8
  • 64
  • 88
MTT
  • 5,113
  • 7
  • 35
  • 61

5 Answers5

377

maven-compiler-plugin it's already present in plugins hierarchy dependency in pom.xml. Check in Effective POM.

For short you can use properties like this:

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

I'm using Maven 3.2.5.

Lii
  • 11,553
  • 8
  • 64
  • 88
leocborges
  • 4,799
  • 5
  • 32
  • 38
  • 1
    Which way is "best"? This one is less verbose compared to the selected answer, but it seems sort of hidden. Even the Maven [site documentation](https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html) shows using the plugin. – mkobit Oct 15 '15 at 16:34
  • 2
    @mkobit I guess this is a personal choice. If I need to configure something else besides the compiler and source version I prefer to do that in plugins section. – leocborges Oct 15 '15 at 17:26
  • 1
    Good point with the "configure something else". If I want to use something like [*google/error-prone*](https://github.com/google/error-prone), I would have to use the plugins section way. – mkobit Oct 24 '15 at 04:22
  • 5
    This is now the first method on the Maven documentation page that @mkobit gave. Much nicer, and you don't have to pin the version of the compiler plugin. – Danila Piatov Feb 19 '18 at 18:49
  • This worked for me after I recreated the project in IntelliJ. I had to reopen the project in IntelliJ by opening the project's pom file as a new project which I am assuming recreated the project. – user674669 Dec 23 '18 at 07:45
  • **Update:** See [Answer by davidxxx](https://stackoverflow.com/a/52103890/642706) for the modern replacement of `source` and `target` with `release` in Maven 3.6 and later. – Basil Bourque Jul 28 '20 at 01:25
312
<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>

See the config page for the maven compiler plugin:

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

Oh, and: don't use Java 1.3.x, current versions are Java 11 or 17.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • 4
    You also might want to add a check on the JDK version you are using to run Maven with the Maven Enforcer Plugin: http://maven.apache.org/enforcer/maven-enforcer-plugin/ I find this very convenient as I have to support different projects that are on different JDK versions. It avoids that you compile with Java 8 on a Java 7 project for example. – Wim Deblauwe Jun 02 '14 at 14:15
  • 2
    The plugin version is missing. It wouldn't throw an error but it's strongly recommended, to set the version there. The current version is 3.3 – Lukas Fink Jun 24 '15 at 06:00
  • 1
    @LukasWerner you're right of course. but if I add a version I have to edit the question every few months. went for a compromise :-) – Sean Patrick Floyd Jun 24 '15 at 08:22
  • PLEASE clarify... What goes into this tag (whatever version is current)? Is it something like 1.7? – edjm Aug 11 '15 at 12:48
  • 2
    @Elijah no, that would be the java language version. I am talking about the plugin version. you can find that through this link: http://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin – Sean Patrick Floyd Aug 11 '15 at 13:23
  • To add to this, the plugin version seems to be really important! I thought I could just leave it out and maven would try to find the latest version, but it doesn't and I kept getting the error about the source being 1.3 (using maven 2.2.1). – Fodder Oct 15 '15 at 20:50
  • @Fodder a) you should no longer be using Maven 2.x, it's end-of-life. b) with no explicit versions, maven falls back to the versions defined in the implicit global super pom. – Sean Patrick Floyd Oct 15 '15 at 20:51
  • @SeanPatrickFloyd I would like to stop using Maven 2, but unfortunately, that's what I have to live with until our project can be moved to Maven 3. Not that moving to 3 is difficult, and is on our tech debt list, but unfortunately, I don't control the priorities. :( Edit to add: though maybe this would be a good time to revisit! – Fodder Oct 15 '15 at 21:21
  • Thank you! To get it working I also had to enable "Use plugin registry" in Settings->Build, Execution, Deployment->Maven. – Martin Pabst Mar 20 '17 at 21:04
  • why is 1.7 not working as with source and target tags? – fall Jun 29 '17 at 03:48
  • **Update:** See [Answer by davidxxx](https://stackoverflow.com/a/52103890/642706) for the modern replacement of `source` and `target` with `release` in Maven 3.6 and later. – Basil Bourque Jul 28 '20 at 01:24
  • https://stackoverflow.com/a/31675559/663432 is preferred way, for compilers starting from JDK9 specifying maven.compiler.release property as well is recommended – mirec Aug 03 '23 at 10:52
21

Generally you don't want to value only the source version (javac -source 1.8 for example) but you want to value both the source and the target version (javac -source 1.8 -target 1.8 for example).
Note that from Java 9, you have a way to convey both information and in a more robust way for cross-compilation compatibility (javac -release 9).
Maven that wraps the javac command provides multiple ways to convey all these JVM standard options.

How to specify the JDK version?

Using maven-compiler-plugin or maven.compiler.source/maven.compiler.target properties to specify the source and the target are equivalent.

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

and

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

are equivalent according to the Maven documentation of the compiler plugin since the <source> and the <target> elements in the compiler configuration use the properties maven.compiler.source and maven.compiler.target if they are defined.

source

The -source argument for the Java compiler.
Default value is: 1.6.
User property is: maven.compiler.source.

target

The -target argument for the Java compiler.
Default value is: 1.6.
User property is: maven.compiler.target.

About the default values for source and target, note that since the 3.8.0 of the maven compiler, the default values have changed from 1.5 to 1.6.

<release> tag — new way to specify Java version in maven-compiler-plugin 3.6

You can use the release argument :

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

You could also declare just the user property maven.compiler.release:

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

But at this time the last one will not be enough as the maven-compiler-plugin default version you use doesn't rely on a recent enough version.

The Maven release argument conveys release to the Java compiler to access the JVM standard option newly added to Java 9, JEP 247: Compile for Older Platform Versions.

Compiles against the public, supported and documented API for a specific VM version.

This way provides a standard way to specify the same version for the source, the target and the bootstrap JVM options.
Note that specifying the bootstrap is a good practice for cross compilations and it will not hurt if you don't make cross compilations either.

Which is the best way to specify the JDK version?

Java 8 and below

Neither maven.compiler.source/maven.compiler.target properties or using the maven-compiler-plugin is better. It changes nothing in the facts since finally the two ways rely on the same properties and the same mechanism : the maven core compiler plugin.

Well, if you don't need to specify other properties or behavior than Java versions in the compiler plugin, using this way makes more sense as this is more concise:

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

Java 9 and later

The release argument (third point) is a way to strongly consider if you want to use the same version for the source and the target.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
davidxxx
  • 125,838
  • 23
  • 214
  • 215
11

I faced same issue in eclipse neon simple maven java project

But I add below details inside pom.xml file

   <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>

After right click on project > maven > update project (checked force update)

Its resolve me to display error on project

Hope it's will helpful

Thansk

Vijay Gajera
  • 1,306
  • 11
  • 13
  • This worked for me when I was trying to build the SqlServerSample app for the Microsoft JDBC Driver for SQL Server. – chris Jun 02 '17 at 23:36
1
<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>