Java project in Eclipse: The type java

The Reference error “The type java.lang.Object cannot be resolved” is a common issue in Eclipse caused due to either misconfigured JDK settings or corrupted project files. This blog will explain this issue in detail the Java project in Eclipse, its cause, and possible solutions to resolve the issue, along with understanding them with relevant examples.

Table of Content

Why does this error happen?

The Reference errors“The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files” generally occur in Eclipse in two cases, i.e either the project is unable to locate essential Java runtime libraries, or the Java Development Kit (JDK) is not properly configured. This issue commonly arises due to the following scenarios:

  • Either JDK is not installed or incorrectly linked in Eclipse.
  • The project uses an older or unsupported Java version.
  • The project may contain a corrupted or missing .classpath or .project file.
  • The project may be either misconfigured or incomplete environment variables (e.g., JAVA_HOME).

Causes of ReferenceError

The following are the causes of the reference error:

  • Eclipse IDE fails to compile the project.
  • Essential Java classes, including java.lang.Object, can’t be referenced.
  • Developers can’t execute or debug the code until the issue is resolved.

Error Message

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

Error Type

Reference Error

Example

The following code illustrates the issue caused by a misconfiguration in the JDK or build path in Eclipse, or the compiler cannot locate the java.lang.Object class.

public class JavaClass {

    public static void main(String[] args) {

        System.out.println("Hello World!");

    }

}

Output:

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

Steps to Resolve

The below steps will be followed to resolve the issue:

Step 1: Verify JDK Configuration

  • Go to Eclipse Application.
  • Go to Window > Preferences > Java > Installed JREs.
  • Make sure that the compatible version of JDK is selected and configured.
    • If not, click Add > Standard VM and provide the JDK installation path.

Step 2: Update Project JRE

  • Right-click on the project from the Project Directory.
  • Select Build Path > Configure Build Path.
  • Under the Libraries tab, check the JRE System Library.
    • If incorrect, click Add Library > JRE System Library and select the appropriate JDK.

Step 3: Clean and Rebuild the Project

  • Go to Project > Clean…
  • Select the project and click OK.
  • This forces Eclipse to recompile and resolve dependencies.

Step 4: Adjust Compiler Compliance Level

  • Right-click the project and select Properties.
  • Go to Java Compiler and ensure the compliance level matches the JDK version.
  • Apply changes and restart the IDE.

Step 5: Validate eclipse.ini Configuration

  • Open the eclipse.ini file located in the Eclipse installation folder.
  • Add or update the -vm argument to point to your JDK installation:
    -vm
    C:/Program Files/Java/jdk-17/bin/javaw.exe
  • Restart Eclipse after saving the changes.

Step 6: Re-import the Project

  • Delete the project from Eclipse (without deleting files from the disk).
  • Re-import the project via File > Import > Existing Projects into Workspace.

Example

The following code illustrates the fixing of the JDK configuration or correcting the project’s JRE System Library.

public class JavaClass {

    public static void main(String[] args) {

        System.out.println("Hello World!");

    }

}

Output:

Hello World!

Conclusion

We have seen that the error “The type java.lang.Object cannot be resolved” occurs due to misconfigured JDK settings or corrupted project files. Installing the correct JDK version, updating the project’s JRE, and cleaning the build path, can help to quickly resolve the problem. Setting the appropriate environment setup ensures smooth development and eliminates unnecessary interruptions.

FAQs

Q1. What causes the error "The type java.lang.Object cannot be resolved"?

This error typically occurs when the JDK is not correctly configured in Eclipse, or the project’s build path is missing the JRE System Library.

Q2. How to check if Eclipse is using the correct JDK?

Go to Window > Preferences > Java > Installed JREs and verify that the JDK is listed and selected.

Q3. Why does cleaning the project resolve the issue?

Cleaning the project forces Eclipse to recompile and resolve dependencies, fixing issues related to outdated or broken build paths.

Q4. Is it possible to fix this issue without reinstalling Eclipse?

Yes, the issue is usually resolved by updating the JDK configuration or the project’s build path. Reinstalling Eclipse is rarely necessary.

Q5. What should we do if the error persists after trying all steps?
  • Verify your JAVA_HOME and PATH environment variables.
  • Re-import the project into Eclipse.
  • Check for corrupted workspace settings by starting Eclipse with the -clean flag:

eclipse -clean

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner