0

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.stack.imgur.com/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.

Opt
  • 4,774
  • 3
  • 28
  • 28
  • Is the Apache library jar on your classpath when you execute the project jar? Is it packaged inside your project jar and can you verify it by exploring its contents? – Ryan J Jul 17 '14 at 19:05
  • Not explicitly in the command itself but shouldn't intellij bundle it automatically? I've trove in there but I don't need to put that in my classpath. – Opt Jul 17 '14 at 19:27
  • Please show us the actual error you see – Daniel Kaplan Jul 17 '14 at 19:29
  • 1
    Maven == problem solved – Software Engineer Jul 17 '14 at 19:33
  • I've never used IntelliJ's package feature, I've only used Maven so I couldn't say for sure if dependent libs are packaged with your jar. However, if you're getting a no class found error, it's because the classloader can't find it. It's not on your classpath, or it's not in the jar. – Ryan J Jul 17 '14 at 19:34
  • Also, show us a screenshot of your "Add to Library" tab. – Daniel Kaplan Jul 17 '14 at 19:37
  • @EngineerDollery: I'm not wedded to IntelliJ's manager but I don't know if there's a quick way of using Maven from within Intellij. – Opt Jul 17 '14 at 20:04
  • @Opt Maven is SO easy to use. There is even a maven tab to the right. – Nick Humrich Jul 17 '14 at 20:06

1 Answers1

1

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.

Nick Humrich
  • 14,905
  • 8
  • 62
  • 85
  • I see. Why is trove not a problem then but apache-commons-math is? – Opt Jul 17 '14 at 20:19
  • @Opt Maybe the code is hitting apache commons first. Have you tried running it without the math dependency and with trove? – Nick Humrich Jul 17 '14 at 20:21
  • Yes, that's what I was doing in the beginning (apache was added later on). That worked completely fine. – Opt Jul 17 '14 at 20:22
  • @Opt Can you add to your question the command you are using to do the jaring? – Nick Humrich Jul 17 '14 at 20:26
  • @NickHumrich i am getting same error for eclipse iam using poi 4.1.1 and math jar is one of its dependecies, what can i do to resolve ? – Dandy Jul 02 '23 at 12:43