0

I have used three external jars:

  1. commons-math3-3.0.jar
  2. jcommon-1.0.23.jar
  3. jfreechart-1.9.19-experimental.jar

When I try to create a jar for my project, I get an error :

Exception in thread "main" java.lang.NoClassDefFoundError:

org/jfree/data/xy/XYDataset

       at Main.main(Main.java:124) Caused by: 

java.lang.ClassNotFoundException: org.jfree.data.xy.XYDataset

    at java.net.URLClassLoader.findClass(Unknown Source)

    at java.lang.ClassLoader.loadClass(Unknown Source)

    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

    at java.lang.ClassLoader.loadClass(Unknown Source)

    ... 1 more

Using IntelliJ to do the same.

I tried the following where :

  1. Go to file > Project Structure > Modules> Dependencies
  2. Click on + and add all the external libraries with scope as "Compile"
  3. Click ok and Apply
  4. Build Artifacts and jar is created.

Please suggest.

learner66666666
  • 167
  • 1
  • 1
  • 15
  • Please see http://stackoverflow.com/a/42200519/104891. – CrazyCoder Jul 31 '18 at 20:03
  • This is not a duplicate as it is different. The given solution in the link doesnot work. – learner66666666 Jul 31 '18 at 20:07
  • See also http://stackoverflow.com/a/42660624/104891 and https://stackoverflow.com/a/45169655/104891. – CrazyCoder Jul 31 '18 at 20:16
  • The issue is that the manifest file has no reference to the external jars. when I unpack the jar I can see the external jars but how will the compiler get the references if manifest file doenot point to it – learner66666666 Jul 31 '18 at 20:49
  • Jars inside jars will not work anyway, IDE adds references or unpacks the jars automatically if the artifact is configured properly (as described in http://stackoverflow.com/a/42200519/104891, either side by side and linked via manifest, or **extracted** inside the same jar). Note that some other manifest from the dependent jar may override the original one, this can be fixed by changing the order of files in the artifact, see https://youtrack.jetbrains.com/issue/IDEA-116209#comment=27-656589. – CrazyCoder Jul 31 '18 at 20:50
  • How does the main jar come to know the location of the jar when the main jar is exported and run.. There should be a reference to the external libraries right ? – learner66666666 Jul 31 '18 at 21:05
  • IntelliJ IDEA generates Manifest file with the links to other jars when you are using the **link the dependent jars via the Manifest.MF** option for the artifact. `HelloWithDependencies.zip` sample project referenced in the first comment has the sample project. Build the artifact, check `out\linked\HelloWithDependencies.jar\META-INF\MANIFEST.MF` file, it will have the link to the dependent jar: `Class-Path: log4j-1.2.17.jar`. – CrazyCoder Jul 31 '18 at 21:27
  • If it's still not clear, [see this screenshot](https://i.imgur.com/pHdoeCh.png). – CrazyCoder Jul 31 '18 at 21:33

0 Answers0