June 10, 2012

Missing class file for java.lang.Object in Eclipse

Having stumbled over this multiple times, usually after surgery on an Eclipse .project file or project properties, it's worth documenting the fix for posterity.

After some surgery on my Eclipse .project and .classpath files, I ran into the following errors in the Problems view:


The project was not built since its build path is incomplete. Cannot find the class file for java.lang.Object. Fix the build path then try building this project


…and…


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


In my case, these errors were shown only for the "last" Java class within a project. The class declaration line of the affected class had the following error tip:


Implicit super constructor Object() is undefined for default constructor. Must define an explicit constructor


This issue is caused by a missing JRE (or JDK) from the classpath, which can be added back in a few simple steps:

  1. Open the affected class file

  2. Under the Project menu, select Properties)

  3. Select Java Build Path in the left pane

  4. Select the Libraries tab in the right pane

  5. Click the Add Library... button

  6. Select JRE System Library from the list

  7. Click the Next > button

  8. Select the appropriate JRE (usually the Workspace default JRE option is fine)

  9. Click the Finish button

  10. Click the OK button to close the project properties.