Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project

JavaIntellij Idea

Java Problem Overview


When creating a new Java project in IntelliJ IDEA, the following directories and files are created:

./projectname.iml
./projectname.ipr
./projectname.iws
./src/

I want to configure IntelliJ IDEA to include my dependency JARs in ./lib/*.jar to the project. What is the correct way to achieve this in IntelliJ IDEA?

Java Solutions


Solution 1 - Java

dialogue in Intellij 20.3

Steps for adding external jars in IntelliJ IDEA:

  1. Click File from the toolbar
  2. Select Project Structure option (CTRL + SHIFT + ALT + S on Windows/Linux, āŒ˜ + ; on Mac OS X)
  3. Select Modules at the left panel
  4. Select Dependencies tab
  5. Select + icon
  6. Select 1 JARs or directories option

Solution 2 - Java

IntelliJ IDEA 15 & 2016
  1. File > Project Structure...

File > Project Structure

or press Ctrl + Alt + Shift + S

  1. Project Settings > Modules > Dependencies > "+" sign > JARs or directories...

Modules > Dependencies > JAR or directories

  1. Select the jar file and click on OK, then click on another OK button to confirm

enter image description here

enter image description here

  1. You can view the jar file in the "External Libraries" folder

enter image description here

Solution 3 - Java

Just copy-paste the .jar under the "libs" folder (or whole "libs" folder), right click on it and select 'Add as library' option from the list. It will do the rest...

enter image description here

Solution 4 - Java

If you are building your project with gradle, you just need to add one line to the dependencies in the build.gradle:

buildscript {
    ...
}
...

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
}

and then add the folder to your root project or module:

enter image description here

Then you drop your jars in there and you are good to go :-)

Solution 5 - Java

You add them as libraries to your module.

I usually have a /lib directory in my source. I put all the JARs I need there, add /lib as a library, and make it part of my module dependencies.

2018 update: I'm using IntelliJ 2017/2018 now.

I'm fully committed to Maven and Nexus for dependency management.

This is the way the world has gone. Every open source Java project that I know of uses Maven or Gradle. You should, too.

Solution 6 - Java

I use this method and it works well:

1- Copy And paste the .jar files under the libs folder.

2- Add compile fileTree(dir: 'libs', include: '*.jar') to dependencies in build.gradle then all the jars in the libs folder will be included..

3- Right click on libs folder and select 'Add as library' option from the list.

Solution 7 - Java

Libraries cannot be directly used in any program if not properly added to the project gradle files.

This can easily be done in smart IDEs like inteli J.

  1. First as a convention add a folder names 'libs' under your project src file. (this can easily be done using the IDE itself)

  2. then copy or add your library file (eg: .jar file) to the folder named 'libs'

  3. now you can see the library file inside the libs folder. Now right click on the file and select 'add as library'. And this will fix all the relevant files in your program and library will be directly available for your use.

Please note:

Whenever you are adding libraries to a project, make sure that the project supports the library

Solution 8 - Java

Some great help found here. However, I still could not make it to work despite loading JAR properly. I found out later that I accidentally created module in the file structure instead of regular folder and this very module was pre-selected in the project setting.

Here is the footprint: >File -> Project Structure -> Modules -> (select proper module if you have more) -> Dependencies -> + -> JAR or Libraries

Solution 9 - Java

While I agree with the previous answers, it's important to note how to access the code of those external libraries.

For example to access a class in the external library, you will want to use the import keyword followed by the external library's name, continued with dot notation until the desired class is reached.

Look at the image below to see how I import CodeGenerationException class from the quickfixj library.

enter image description here

Solution 10 - Java

  1. File > Project Structure
  2. Project Settings > Modules > Dependencies (Select one of)
    • 1 JARs or Directories...
    • 2 Library...
    • 3 Module Dependency... enter image description here
  3. Apply + Ok

enter image description here

  1. Import into java class

Solution 11 - Java

You can put the JAR in the libs folder and add it from there. This can be done in 2 different ways in IntelliJ:

  1. Right-click on the libs folder and add from there:

enter image description here

  1. Add the JAR from the project structure:

enter image description here

Solution 12 - Java

If you are building your project with maven, you just need to add one line to the dependencies in the pom.xml:

<dependency>
     <groupId>com.xxx</groupId>
     <artifactId>xxxx-server</artifactId>
     <version>1.0.0</version>
     <scope>system</scope>
     <systemPath>${pom.basedir}/src/libs/xxx-server-1.0.0.jar</systemPath>
</dependency>

and then add the folder to your root project or module:

enter image description here

this is my personal experiences. I wish they would help you

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionknorvView Question on Stackoverflow
Solution 1 - JavaCambiumView Answer on Stackoverflow
Solution 2 - JavaROMANIA_engineerView Answer on Stackoverflow
Solution 3 - JavagurkanView Answer on Stackoverflow
Solution 4 - JavacesardsView Answer on Stackoverflow
Solution 5 - JavaduffymoView Answer on Stackoverflow
Solution 6 - JavaAli HesariView Answer on Stackoverflow
Solution 7 - JavaKeet SugathadasaView Answer on Stackoverflow
Solution 8 - JavaRollandView Answer on Stackoverflow
Solution 9 - JavakguiView Answer on Stackoverflow
Solution 10 - JavaCuriosCoderView Answer on Stackoverflow
Solution 11 - JavaArefeView Answer on Stackoverflow
Solution 12 - JavaqingmuView Answer on Stackoverflow