Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same source code, Eclipse build success but Maven (javac) fails

Keep getting this error when compiling using Maven:

type parameters of <X>X cannot be determined; no unique maximal instance exists for type variable X with upper bounds int,java.lang.Object

Generics type interference cannot be applied to primitive types. But I thought since Java5, boxing/unboxing mechanism works seamlessly between primitive types and wrapper classes.

In any case, the strange thing is Eclipse doesn't report any errors and happily compiles. I'm using JDK1.6.0_12. What could possibly be the problem here?

like image 628
EnToutCas Avatar asked Oct 22 '09 19:10

EnToutCas


People also ask

Why does Maven build failure in eclipse?

The error message indicates that Maven is unable to find the Java compiler, which comes only with a JDK and not with a JRE.

How to Run Maven build in Eclipse?

Building and Running the Maven Project in Eclipse To run the maven project, select it and go to “Run As > Java Application”. In the next window, select the main class to execute. In this case, select the App class and click on the Ok button. You will see the “Hello World” output in the Console window.

How do I run a Maven project after building success?

java. Update the App class to use Util class. Now open the command console, go the C:\MVN\consumerBanking directory and execute the following mvn command. After Maven build is successful, go to the C:\MVN\consumerBanking\target\classes directory and execute the following java command.


1 Answers

This issue can occur when your code is generic and it calls another method that has a generic return type. Sometimes the compiler gets confused trying to figure out how to resolve the method call / return type.

It can be resolved by adding an explicit cast to your code.

// Old code:
public T getValue() {
    return otherMethod();  // otherMethod has the signature: <RT> RT otherMethod() { ... }
}

// New code:
@SuppressWarnings("unchecked")
public T getValue() {
    return (T) otherMethod();   // the cast tells the compiler what to do.
}
like image 146
Dr. Mike Hopper Avatar answered Oct 04 '22 12:10

Dr. Mike Hopper