Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven error with Java 8

Tags:

java

maven

java-8

Getting an error with Maven and Java 8 (jdk1.8.0_45). This issue does not occur with Java 7.

MCVE

Create a sample maven project. For example:

mvn archetype:create -DgroupId=testinovke -DartifactId=testinvoke

Create the following content in the generated App.java file

package testinovke;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

public class App {

    public static MethodHandles.Lookup lookup;

    public static class Check {
        public void primitive(final int i){
        }
        public void wrapper(final Integer i){
        }
    }


    public static void main(String[] args) throws Throwable {
        Check check = new Check();

        MethodType type = MethodType.methodType(void.class, int.class);

        MethodHandle mh = lookup.findVirtual(Check.class, "primitive", type);
        mh.invoke();
    }
}

Compile the maven project:

mvn clean compile

Output

Get the following error:

testinvoke/src/main/java/testinovke/App.java:[25,18] method invoked with incorrect number of arguments; expected 0, found 1

Tried it with both Maven 3.0.4 and 3.3.3. This issue does not exist if I directly compile against App.java using Javac command.

like image 412
codedabbler Avatar asked Jun 18 '15 13:06

codedabbler


2 Answers

Add plugin configuration for the compiler:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <verbose>true</verbose>
                <fork>true</fork>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin> 
like image 94
Jilles van Gurp Avatar answered Oct 21 '22 19:10

Jilles van Gurp


Another solution is adding these properties:

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

to your pom.xml, and the plugins will pick these up automatically.

like image 39
meskobalazs Avatar answered Oct 21 '22 20:10

meskobalazs