Adrian
Courrèges

"The type java.lang.Object cannot be resolved." with Eclipse

The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files

This is the nice error message I got from Eclipse after the CVS checkout of a project. This error is kind of annoying because Object is the base of almost all Java entities, without it it’s impossible to create any Java class.
It took me a while before figuring out it was actually a bug with Eclipse. Here is how to get rid of it.

Eclipse bug

During a project creation, Eclipse generates a .classpath file within the project directory. In my case this file looked like this:

.classpath (incorrect)
1
2
3
4
5
6
7
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path=""/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/
    org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/java5">
    <classpathentry kind="output" path="">
</classpath>

The fifth line is the problem: it contains data which has nothing to do here. It has been mistakenly added by Eclipse.
It is just necessary to remove: org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/java5

This is the correct content:

.classpath (correct)
1
2
3
4
5
6
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="">
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
    <classpathentry kind="output" path="">
</classpath>

This solved the issue in my case. :)

I’m not sure if the bug is linked to the version of Eclipse (3.1).
At first I had some suspicions about the JVM used by Eclipse.
Many Linux distributions come with GCJ as the default JVM, usually I force the use of the Sun JRE 1.5 with the command:

$ /usr/bin/eclipse -vm /usr/lib/jvm/java-1.5.0-sun/bin/java

Edit: actually the problem only appears if you specify a custom version of the JVM to use during the project creation. To avoid the bug you can create the project and only after the creation you affect a particular version for the JVM within the project options.

Comments