13

How can I add an external library to a project in IntelliJ IDEA so that when I build an artifact it still has access to the classes in the library?

I have created a new Jar artifact from Project Structure, then added the external JAR to the Libraries, then checked it in the Modules List, and finally added it to the Output for the Artifact. None of these work. When I build and try running my application, it throws an error:

Exception in thread "main" java.lang.NoClassDefFoundError: <path of the class trying to use>

What am I missing, or am I doing this completely wrong?

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904

2 Answers2

22

You have 2 options here:

  • extract the dependency into the artifact jar so that the app is the single executable jar with all the dependencies
  • link the dependent jars via the Manifest.MF and copy them near the application main jar

I've prepared a sample project that demonstrates both approaches: HelloWithDependencies.zip.

The artifacts are produced into out\single and out\linked directories.

Relevant configurations:

single

linked

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • 2
    You just helped me fix a problem I've spent over 25 hours, maybe 2 weeks, trying to decipher. Thank you very much! Also, this works and all, but what's the backbone behind it? Does IntelliJ use Maven or Ant or anything like that? – Shivashriganesh Mahato Feb 14 '17 at 08:16
  • 2
    You can use projects based on Maven, yes, but it's unrelated to the situation you've got. When you add new dependencies to the project they are not automatically added to the artifacts you already have, you need to review the artifact configuration again and add all the missing libraries required for running your app from a jar. Another common mistake is to include one jar inside another jar which will not work, the jar must be either unpacked inside the main jar or copied near it and linked via the manifest (don't forget to add any new jars to the manifest in that case). – CrazyCoder Feb 14 '17 at 08:20
  • 1
    In a Maven based project one can use Maven plugins for creating jars with dependencies and you will produce the final artifacts by running the Maven goal instead of using IntelliJ IDEA artifacts configuration. This way all the new dependencies you add to `pom.xml` will automatically appear in the jar produced by Maven. Pretty much the same applies to the Gradle based projects. – CrazyCoder Feb 14 '17 at 08:23
  • Where would resources, like images, fit into this? – Shivashriganesh Mahato Feb 16 '17 at 07:04
  • Resources normally go into the `resources` directory (the one configured as Resources root). In the sample project it's the same directory where you see `log4j.properties` file. You can add more resources roots if you need and the files from these directories will appear in the compiler output, so you don't have to adjust the artifact configuration. – CrazyCoder Feb 16 '17 at 08:11
  • When i try JAR method and try to click on jar file it says invalid or corrupt file. Other one does not include libraries inside – Mert Serimer Jul 13 '17 at 11:40
  • @MertSerimer you can download the sample project from the link in the answer and see yourself that it works. If it doesn't work for your specific project, you will have to provide the [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) in order to get any help. – CrazyCoder Jul 13 '17 at 11:41
  • I am trying what you did in your project and it does not work. Your single jar also can not be opened. Mine gives invalid or corrupt. Yours do nothing – Mert Serimer Jul 13 '17 at 11:49
  • @MertSerimer no, my sample jar [works just fine](http://i.imgur.com/r3rdl2O.png). It's a console application so you need to run it in the console to see the results. – CrazyCoder Jul 13 '17 at 11:50
  • @CrazyCoder I have the same question, I encountered `NoClassDefFoundError` error when running my .jar file, and I checked my project structure page, but don't know how to do to fix it, can you explain in more detail? Or maybe we could use chatting place to show you my situation if you need, thanks! – Alanight Dec 13 '17 at 02:53
  • @CrazyCoder I've fixed it! After re-add the .jar, and choose **extract to the target JAR** option, it could work, thanks! – Alanight Dec 13 '17 at 04:09
1

If you are using maven to build your application then this is not the correct way to add external library. You should either

  1. Do an install of your library like below mvn install:install-file -Dfile=myJar.jar -DgroupId=com.yourproject -DartifactId=yourproject -Dversion={version} -Dpackaging=jar.
  2. Use system path like explained here.

Option 1 is prefered since you don't have to keep jar in your project.

Community
  • 1
  • 1
GauravJ
  • 2,162
  • 1
  • 22
  • 28
  • I actually don't know if Maven is being used. I haven't created any build files. Not sure how IDEA does it, but my assumption was that a build file isn't needed. All there is is a MANIFEST.MF file. – Shivashriganesh Mahato Feb 13 '17 at 06:43