Adding an External Library to an Artifact Bank in IntelliJ IDEA

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

I created a new Jar artifact from the project structure, and then added an external JAR to the libraries, then checked it in the module list and finally added it to the output for the artifact. None of these works. When I create and try to start the application, it gives an error message:

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

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

+5
source share
2 answers

You have 2 options here:

  • Extract the dependency in the jar of the artifact so that the application is a single executable jar with all the dependencies.
  • link the dependent banks through Manifest.MF and copy them next to the main application file

I prepared an example project that demonstrates both approaches: HelloWithDependencies.zip .

Artifacts are created in the out\single and out\linked directories.

Relevant configurations:

single

linked

+11
source

If you use maven to create your application, this is not the right way to add an external library. You must either

  • Install your library as shown below mvn install:install-file -Dfile=myJar.jar -DgroupId=com.yourproject -DartifactId=yourproject -Dversion={version} -Dpackaging=jar .
  • Use the system path as described here .

Option 1 is preferable, since you do not need to keep the jar in your project.

+1
source

Source: https://habr.com/ru/post/1268520/


All Articles