1

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>

Lin Du
  • 88,126
  • 95
  • 281
  • 483
kyrers
  • 489
  • 1
  • 6
  • 22
  • Could you please add your run configuration for that jar? It looks like the `stdlib.jar` is not added to the classpath. Therefore, it cannot be found. – Peter Apr 29 '19 at 18:24
  • Please, don't do screenshots. Post your code and error message. Also, it would be helpful if you post a link to download this lib. – Sergei Sirik Apr 29 '19 at 18:25
  • First of all you are using stdlib-package.jar, not stdlib.jar(seems like this one contains source code in default package). Second, just do right click on your project -> Open Module Settings -> Libraries -> "+" -> Java -> specify your stdlib-package.jar -> Apply OK. I just checked, it works for me this way. – Sergei Sirik Apr 29 '19 at 18:42
  • @SergeiSirik that's what I first tried and it doesn't work. What I've found is that when I try to run the stdlib-package.jar it says that there is no main manifest attribute, but I can't edit it. My hospital-simulator.jar has the main class attribute. – kyrers Apr 29 '19 at 19:11
  • @Peter I believe I've added everything you asked for. – kyrers Apr 29 '19 at 19:24
  • It looks like you're trying to run a jar created from your project. This will only work if you either include the classes from the library in your project jar (i.e., create a 'fat jar'), or include stdlib.jar in your runtime classpath (i.e., run your project with `java -cp /path/to/stdlib.jar:/path/to/hospital-simulator.jar com.edgelab.hospital.Application`. – OhleC Apr 29 '19 at 20:06

1 Answers1

0

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.

kyrers
  • 489
  • 1
  • 6
  • 22
  • 1
    You were trying to run the JAR artifact which didn't include the dependency, see https://www.jetbrains.com/help/idea/artifacts.html and https://stackoverflow.com/a/42200519/104891 for the example. – CrazyCoder Apr 29 '19 at 20:59
  • @CrazyCoder I rebuilt the artifact several times, and I believe that can't be the reason due to the following: To verify that that wasn't the case, when I created the new project I built the JAR artifact and only after added the stdlib-package.jar. I then rebuild the JAR artifact and it worked. – kyrers Apr 30 '19 at 14:55
  • IntelliJ IDEA doesn't update artifact automatically when you add a new library to the project. You need to change the artifact configuration manually before rebuilding it. – CrazyCoder Apr 30 '19 at 17:39
  • Yes, I know and I did. Believe me. I sincerely have no idea what happened. Might not even be IntelliJ related at this point. I honestly don't know. – kyrers Apr 30 '19 at 17:43