简体   繁体   中英

Adding external library to artifact jar in IntelliJ IDEA

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?

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:

单身的

链接

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM