Java: Unresolved compilation problem

Java

Java Problem Overview


What are the possible causes of a "java.lang.Error: Unresolved compilation problem"?

Additional information:

I have seen this after copying a set of updated JAR files from a build on top of the existing JARs and restarting the application. The JARs are built using a Maven build process.

I would expect to see LinkageErrors or ClassNotFound errors if interfaces changed. The above error hints at some lower level problem.

A clean rebuild and redeployment fixed the problem. Could this error indicate a corrupted JAR?

Java Solutions


Solution 1 - Java

(rewritten 2015-07-28)

The default behavior of Eclipse when compiling code with errors in it, is to generate byte code throwing the exception you see, allowing the program to be run. This is possible as Eclipse uses its own built-in compiler, instead of javac from the JDK which Apache Maven uses, and which fails the compilation completely for errors. If you use Eclipse on a Maven project which you are also working with using the command line mvn command, this may happen.

The cure is to fix the errors and recompile, before running again.

The setting is marked with a red box in this screendump:

Eclipse Preferences under OS X

Solution 2 - Java

try to clean the eclipse project

Solution 3 - Java

you just try to clean maven by command

mvn clean

and after that following command

mvn eclipse:clean eclipse:eclipse

and rebuild your project....

Solution 4 - Java

Your compiled classes may need to be recompiled from the source with the new jars.

Try running "mvn clean" and then rebuild

Solution 5 - Java

The major part is correctly answered by Thorbjørn Ravn Andersen.

This answer tries to shed light on the remaining question: how could the class file with errors end up in the jar?

Each build (Maven & javac or Eclipse) signals in its specific way when it hits a compile error, and will refuse to create a Jar file from it (or at least prominently alert you). The most likely cause for silently getting class files with errors into a jar is by concurrent operation of Maven and Eclipse.

If you have Eclipse open while running a mvn build, you should disable Project > Build Automatically until mvn completes.

EDIT: Let's try to split the riddle into three parts:

> (1) What is the meaning of "java.lang.Error: Unresolved compilation > problem"

This has been explained by Thorbjørn Ravn Andersen. There is no doubt that Eclipse found an error at compile time.

> (2) How can an eclipse-compiled class file end up in jar file created > by maven (assuming maven is not configured to used ecj for > compilation)?

This could happen either by invoking Maven with no or incomplete cleaning. Or, an automatic Eclipse build could react to changes in the filesystem (done by Maven) and re-compile a class, before Maven proceeds to collect class files into the jar (this is what I meant by "concurrent operation" in my original answer).

> (3) How come there is a compile error, but mvn clean succeeds?

Again several possibilities: (a) compilers don't agree whether or not the source code is legal, or (b) Eclipse compiles with broken settings like incomplete classpath, wrong Java compliance etc. Either way a sequence of refresh and clean build in Eclipse should surface the problem.

Solution 6 - Java

I had this error when I used a launch configuration that had an invalid classpath. In my case, I had a project that initially used Maven and thus a launch configuration had a Maven classpath element in it. I had later changed the project to use Gradle and removed the Maven classpath from the project's classpath, but the launch configuration still used it. I got this error trying to run it. Cleaning and rebuilding the project did not resolve this error. Instead, edit the launch configuration, remove the project classpath element, then add the project back to the User Entries in the classpath.

Solution 7 - Java

I got this error multiple times and struggled to work out. Finally, I removed the run configuration and re-added the default entries. It worked beautifully.

Solution 8 - Java

  1. Just try to include package name in eclipse in case if you forgot it
  2. Import all packages before using it, EX: import java.util.Scanner before using Scanner class.
  3. These improvements might work and it will not give Java: Unresolved compilation problem anymore.
  4. Also make sure to check compiler compliance level and selected jdk version is same

Solution 9 - Java

As a weird case, I encountered such an exception where the exception message (unresolved compilation bla bla) was hardcoded inside of generated class' itself. Decompiling the class revealed this.

Solution 10 - Java

I had the same issue using the visual studio Code. The root cause was backup java file was left in the same directory.

Removed the backup java file When the build failed, selected the Fix it, it cleaned up the cache and restarted the workSpace.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFrankView Question on Stackoverflow
Solution 1 - JavaThorbjørn Ravn AndersenView Answer on Stackoverflow
Solution 2 - Javamicahli123View Answer on Stackoverflow
Solution 3 - JavajaideepView Answer on Stackoverflow
Solution 4 - JavaSteve PearsonView Answer on Stackoverflow
Solution 5 - JavaStephan HerrmannView Answer on Stackoverflow
Solution 6 - JavadjbView Answer on Stackoverflow
Solution 7 - JavaVinhView Answer on Stackoverflow
Solution 8 - Javakrishna prasad peramView Answer on Stackoverflow
Solution 9 - JavayılmazView Answer on Stackoverflow
Solution 10 - JavaspmView Answer on Stackoverflow