Search code examples
javaintellij-ideadependencieslibrariesnoclassdeffounderror

How to fix the NoClassDefFoundError in IntelliJ Idea after having tried every way to add stdlib.jar


I've created a simple Java project in IntelliJ Idea 2018 and I'm trying to add stdlib to my project.

After spending hours investigating online I tried doing so the following ways:

  1. Create a libs folder inside the src folder, copy and paste the stdlib.jar and right-click it and select Add as a Library;
  2. Go to Project Structure -> Modules -> Dependencies -> + JARs or directories and selecting the stdlib.jar

I found people complaining about doing this but IntelliJ was not suggesting the import needed to use StdOut, for example. However, when I add the stdlib.jar file and package it, IntelliJ instantly suggests the import I should use, and there are no errors whatsoever.

Still, when I try to run the project it invariantly runs into the NoClassDefFoundError.

I've spent hours investigating this, trying different ways to add the library and import it, but the outcome is always the same.

I've attached a print screen that shows my current project structure, import, and use of the stdlib.jar.

Really hope you guys can help me. Thanks!

Link to download stdlib-package.jar - https://introcs.cs.princeton.edu/java/stdlib/stdlib-package.jar

MANIFEST.MF

Manifest-Version: 1.0 
Main-Class: com.edgelab.hospital.Application

HOSPITAL_SIMULATOR_JAR.xml

<component name="ArtifactManager">
  <artifact type="jar" name="hospital-simulator:jar">
    <output-path>$PROJECT_DIR$</output-path>
    <root id="archive" name="hospital-simulator.jar">
      <element id="module-output" name="HospitalSimulator" />
    </root>
  </artifact>
</component>

stdlib-package.xml

<component name="libraryTable">
  <library name="stdlib-package">
    <CLASSES>
      <root url="jar://$PROJECT_DIR$/../stdlib-package.jar!/" />
    </CLASSES>
    <JAVADOC />
    <SOURCES>
      <root url="jar://$PROJECT_DIR$/../stdlib-package.jar!/" />
    </SOURCES>
  </library>
</component>


Solution

  • So, I finally solved it. Here's what was wrong: Nothing.

    After searching the web I found (for some reason I can't post the link) an answer which suggested copying my code, deleting the project and creating a new one. So, I deleted my project, created a new one with the same code, created the same artifacts, added the stdlib-package.jar as a library and rebuilt the hospital-simulator.jar and it worked instantly.

    I can't explain why this works, as I have no idea. I literally did the same thing by the same order. Hope this helps someone and thanks to everyone who tried to help.