Search code examples
javaintellij-ideaapache-commonsapache-commons-math

Intellij + Apache Math 3.3 giving NoClassDefFound error


I copied the apache Math 3.3 jar library into my intellij project folder and then added it to my project via the "Add to Library" option.

While everything compiles and builds fine, when I run my project's jar, I get NoClassDefFoundError for the classes I import from the apache math library.

Any solutions?

Thanks

Edit:

Below is the error I get

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/math3/stat/descriptive/rank/Percentile
    at Main.main(Main.java:80)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.stat.descriptive.rank.Percentile
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 1 more

The screenshot of the Modules dialog box is here: https://i.sstatic.net/HaRx9.jpg

Edit 2: I installed the apache jar using "Add to Library" and then choosing the "classes" option.

Edit 3: To make the jar, I go to Build -> Build Artifacts -> Build.


Solution

  • The problem is due to this phrase:

    when I run my project's jar

    If you are using java's standard jar mechanism, it doesnt jar up dependencies. So you still need to add dependencies to the class path. IntelliJ's feature is mostly for running using intelliJ's run option. If you instead run the jar through a command line, you will need to add the dependency to the class path.

    If you are jaring with intelliJ, the jar is considered an artifact. You need to add the library to your build artifact. Go to Build > Build Artifacts. Hover over the jar and select Edit.

    You should see the library in the "Available Elements" section. Just double click it so that it gets added to your jar. Then click ok.