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:
Manifest.MF
and copy them near the application main jarI'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
mvn install:install-file -Dfile=myJar.jar -DgroupId=com.yourproject -DartifactId=yourproject -Dversion={version} -Dpackaging=jar
. Option 1 is prefered since you don't have to keep jar in your project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With