91

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?

  • What did you do to cause this error? – Greg Hewgill Jul 14 '09 at 11:28
  • I caused this error by renaming an input parameter and not renaming all occurrences of that parameter in a method. Interestingly `mvn compile` does not cause an error, but running the program does. `mvn clean compile` shows the error during compilation (as expected). – peschü Dec 06 '19 at 12:58

10 Answers10

76

(rewritten 2015-07-28)

Summary: Eclipse had compiled some or all of the classes, and its compiler is more tolerant of errors.

Long explanation:

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

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • That's not my case. I saw this after deploying JAR files (build using Maven) to a server. –  Jul 15 '09 at 11:10
  • 1
    Maybe there's a problem with your Maven build config and it's using .class files from your eclipse output directory. Anyhow, the only time I've seen that error is when running code with compilation errors in eclipse. – Michael Borgwardt Jul 15 '09 at 11:23
  • 1
    In that case your maven build is most likely broken. Try cleaning and recompiling from the command line, with Eclipse shut down. – Thorbjørn Ravn Andersen Jul 15 '09 at 17:48
  • Didn't see you edited the question. I would reinvestigate the Maven build process since it appears to be not completely clean. It should NOT be able to create broken jars! Do you have a build server? – Thorbjørn Ravn Andersen Jul 15 '09 at 18:00
  • I just had the same problem. It looks like, as Michael said, this was Maven picking up the class files generated by Eclipse. – Wouter Coekaerts Aug 27 '09 at 09:08
  • So how do you change that default setting in Eclipse so it doesn't generate code when there are errors? – pacoverflow Jun 19 '14 at 14:58
  • Preferences -> Java -> Compiler -> Errors/Warnings -> Treat above errors like fatal compile errors (make compiled code not executable) – Thorbjørn Ravn Andersen Jun 20 '14 at 08:22
  • I had the same problem just now. Nothing would fix except for Project->Clean and clean all projects in the workspace. After a full rebuild problem was gone. – dmansfield May 14 '15 at 19:08
  • better yet, this extends to JDT.LS. Not using eclipse, but using Eclipse's language server, still makes this happen, but it's much more of a pain to fix. Removing _everything_ eclipse and cleaning makes it go away, so it's definitely Eclipse's fault. Extremely annoying that the presence of Eclipse files is enough to make this happen, though - makes it a pain in the ass to work with other editors relying on JDT.LS for Java completion while using Maven – Zoe Mar 02 '21 at 21:22
  • FYI: Since VisualStudio Code also uses the JDT language server for Java compilation, this problem can also occur with VS code. – Joachim Sauer Feb 01 '23 at 13:16
16

try to clean the eclipse project

micahli123
  • 460
  • 1
  • 7
  • 10
14

you just try to clean maven by command

mvn clean

and after that following command

mvn eclipse:clean eclipse:eclipse

and rebuild your project....

jaideep
  • 1,631
  • 17
  • 19
8

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

Try running "mvn clean" and then rebuild

Steve Pearson
  • 81
  • 1
  • 1
3

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.

Lii
  • 11,553
  • 8
  • 64
  • 88
Stephan Herrmann
  • 7,963
  • 2
  • 27
  • 38
  • I do not think this is a concurrency issue, as it is very systematic and always happens. I think somehow the code gets compiled on demand during loading when it is really used by the JVM. No idea why this is possible. I thought that compile-time-checking is the thing that makes Java an advantage over Python... – peschü Dec 06 '19 at 13:00
  • 1
    @peschü - Well it does do that ... normally. This is caused by an >Eclipse< Java compiler misfeature. They were apparently trying to cater for the case where the developer knows about a compilation error in (say) `method1` but wants to test `method2`. Unfortunately, it is too easy to turn this feature on by accident ... or forget to turn it off. – Stephen C Jun 24 '22 at 14:22
1

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.

djb
  • 4,930
  • 1
  • 34
  • 37
0

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.

Vinh
  • 58
  • 4
  • Well yes ... but doing what the accepted answer says would have worked quicker. It's just a single flag in the preferences. – Stephen C Aug 03 '19 at 02:54
0
  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
0

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.

yılmaz
  • 1,818
  • 13
  • 15
  • Well that's exactly what happens in all cases. The problem was that there were compilation errors when Eclipse generated the `.class` file, but Eclipse had been (previously) configured to generate the `.class` anyway. – Stephen C Jun 24 '22 at 14:18
0

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.

spm
  • 41
  • 6