908

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?

3
  • I am facing the issue while running the program in the terminal in IntelliJ. I have used the import statement to use the classes that I require in the program. While I run it in the IDE, it runs fine. But when I try to compile from the command line % javac MyProgram.java, it says error: cannot find symbol Apr 11, 2020 at 15:50
  • @don_Gunner94 You need to understand how the classpath works and that you may have to provide it more than you expect. Nov 14, 2021 at 13:02
  • 1
    I suggest not to use the approach that you are trying to do. Better you upload your JARs to any artifactory platform, like jFrog and get your artifact from such platform like you do from maven or gradle. I suggest you to follow "The 12-Factor Principles" as "Dependencies (Explicitly declare and isolate the dependencies)" Mar 18, 2022 at 5:41

13 Answers 13

1617

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
19
  • 125
    Also, consider adding a lib folder, and use "Attach Jar Directories", which will add all jars in the lib folder. For a largish project, this keeps you from having to choose each jar individually.
    – joeslice
    Jul 6, 2009 at 18:04
  • 19
    For future reference: that "Add" button was off the right edge of the window and I spent an inordinate amount of time looking for it before finding this post and thinking to resize the window. (This was on OSX 10.6.5...) Dec 10, 2010 at 21:00
  • 9
    @joeslice that only works if the JARs are in the directory root, right? It doesn't seem to work for JARs inside sub-directories inside 'lib'.
    – opyate
    Jun 3, 2011 at 17:21
  • 7
    In IJIdea 13, there is "+"> instead of "add".
    – Val
    Jan 12, 2014 at 14:45
  • 5
    I'm using IDEA 14.0.2. Instead of Add there is + symbol. After clicking + there is 3 options: 1. JARs or directories 2. Library... 3. Module Dependency... Apr 13, 2015 at 6:55
287

IntelliJ IDEA 15 & 2016

  1. File > Project Structure...

    File > Project Structure

    or press Ctrl + Alt + Shift + S

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

    Modules > Dependencies > JAR or directories

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

    enter image description here

    enter image description here

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

    enter image description here

9
  • 1
    I actually create a new Module in the "Modules" pane (named for example "JarsForLogging"), then add all the jars I need (located in some directory far away from the project tree) to this new Module and mark the jars as "Export". Finally, I add the newly created Module to the dependencies of the original Module. A bit more hierarchical that way. Jul 11, 2017 at 16:21
  • In addition: make sure that file system permissions are set correctly for the .jar files. Use chmod to modify them on Linux or MacOS.
    – JJD
    Jan 10, 2018 at 17:06
  • 5
    still works in 2018 (year and IntelliJ version). Thank you. This way I can load and use Armed Bear Common Lisp libraries from within IntelliJ, awesome! Apr 19, 2018 at 4:42
  • Yep. Works well with my IntelliJ as well. Thanks! Jun 10, 2019 at 11:24
  • 2
    @don_Gunner94, via command line, you should also provide the jar like this: javac -cp ".:/my/path/file.jar;" MyProgram.java Apr 11, 2020 at 16:05
170

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

6
  • Simplest and the way we're used to do since time of Eclipse.
    – Sufian
    Jul 3, 2015 at 11:30
  • 5
    Plain and Simple!! Jul 9, 2016 at 2:18
  • 3
    Kind of annoying it still asks for a "name" then, can you just leave any junk in there? Sep 18, 2016 at 21:19
  • This helped me too! May 29, 2018 at 11:35
  • 1
    I am facing the issue while running the program in the terminal in IntelliJ. I have used the import statement to use the classes that I require in the program. While I run it in the IDE, it runs fine. But when I try to compile from the command line % javac MyProgram.java, it says error: cannot find symbol. – Apr 11, 2020 at 15:51
42

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 :-)

3
  • I am facing the issue while running the program in the terminal in IntelliJ. I have used the import statement to use the classes that I require in the program. While I run it in the IDE, it runs fine. But when I try to compile from the command line % javac MyProgram.java, it says error: cannot find symbol. – Apr 11, 2020 at 15:52
  • This should be the correct answer, as it let's you push and sync the libraries needed without any configuration from the ones that pull the project. Set it once and forget. Mar 14, 2021 at 21:02
  • 1
    Wow I can't believe how long it took to find this one line! Not even the tutorial on Gradle has this. Took me a week to get this working (in the little spare time and energy I had) haha. I don't create libraries normally and it was so long ago that I did it before. Mar 25, 2021 at 8:15
24

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.

7
  • Yes, but what if the Maven pom is not called pom.xml? Then it seems like I can't read Maven dependencies. Jul 6, 2015 at 2:25
  • 5
    Convention is to call it pom.xml. Follow convention and rename your file.
    – duffymo
    Jul 6, 2015 at 9:00
  • I still find the need, when the jar has no maven repo ... but, I do not understand how to do this correctly . I understand how to add a directory, I just don't understand how to get it it import the javadocs also too. it works when I add a jar as a single file, but not if I add a directory. Is it simply not possible?
    – ycomp
    Oct 18, 2015 at 15:32
  • This is sound advice. Adding jars manually is time consuming and prone to error. Your dependencies should always be configured "via code", in the pom.xml (for maven) or whatever the equivalent is for gradle. May 28, 2018 at 10:05
  • 2
    All JARs have a repo. If it's not available in a public or private repo you can mvn install it manually in your local .m2. There's no reason to do otherwise.
    – duffymo
    Dec 6, 2018 at 11:59
10

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.

2
  • Thank you, this was the only thing that worked for me!
    – Gregordy
    Apr 16, 2018 at 20:59
  • I am facing the issue while running the program in the terminal in IntelliJ. I have used the import statement to use the classes that I require in the program. While I run it in the IDE, it runs fine. But when I try to compile from the command line % javac MyProgram.java, it says error: cannot find symbol. Apr 11, 2020 at 15:52
7

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

6
  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
5

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

5

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

4

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

1

I'm using Intellij IDEA 2022, with Kotlin.

Created libs folder in the root of the project, and in build.gradle.kts file within dependencies {}:

implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))

Hope this helps someone who came to this post struggling to implement external dependencies, like me ;)

-1

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.