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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
knorv
  • 49,059
  • 74
  • 210
  • 294
  • 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 – don_Gunner94 Apr 11 '20 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. – Thorbjørn Ravn Andersen Nov 14 '21 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)" – Anish Panthi Mar 18 '22 at 05:41

13 Answers13

1616

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
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
Cambium
  • 19,152
  • 3
  • 26
  • 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 06 '09 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...) – Daniel Dickison Dec 10 '10 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 03 '11 at 17:21
  • 1
    There is no "module" in project settings in the latest version 12.1.4 – Raj Jun 16 '13 at 05:55
  • Instead of using ctrl + shift + alt + s, the easier keyboard way is to 1)left click the first line(project name) in the project window. 2)press F4. – duleshi Oct 08 '13 at 03:24
  • 7
    In IJIdea 13, there is "+"> instead of "add". – Val Jan 12 '14 at 14:45
  • IJIdea 13, I got after adding "artifact ...:war exploded: library gson-2 required for module ... is missing from artifact" – Timo May 25 '14 at 11:17
  • 1
    How do you add a directory instead of a jar? When I add a `File Dependency` in the `Dependencies` tab, it will only let me do a single jar, not a directory. – Marcus Jan 05 '15 at 03:46
  • 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... – Ripon Al Wasim Apr 13 '15 at 06:55
  • 3
    Where is "Attach Jar Directories"? – Alkanshel Nov 23 '16 at 18:43
  • 1
    this literally does nothing for me in Intellij ultimate 2016.3 about 90% of the time... I keep trying to add Lombok and sometimes it works.. the majority of the time it doesn't. Then it keeps getting removed. ugh. – SgtPooki Dec 07 '16 at 02:25
  • 1
    After adding the dependencies, should we select the export checkbox? – Tim Siwula Dec 13 '16 at 07:53
  • 1
    In my version of Intellij 2017.1.4, there is no "dependencies" tab under modules. – Paul Jul 23 '17 at 14:26
  • Modules > your module > Dependencies > "+" > JARs or directories. IntelliJ didn't understand JARS in subdirectories. So I added lib/jar_directory_one/ & lib/jar_directory_two/ separately. – Abhishek Saini Feb 20 '18 at 04:52
  • After doing the steps of this answer, I could not import the classes in the Jar. To fix this, I had to do one more step, i.e add the jar to the classpath - https://stackoverflow.com/a/51198781/6648326 – MasterJoe Jul 09 '18 at 01:43
  • If its not being added in this way, check your project's `pom.xml` file as it may already be there as a `` tag. – Alexandru Sep 28 '18 at 16:54
  • A nice thing to note is that IntelliJ comes with a lot of libs. You can navigate to the IntelliJ installation folder, open the "libs" folder and easily add from there. – mpavel Jul 22 '19 at 15:56
  • 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. – don_Gunner94 Apr 11 '20 at 15:50
  • Hi, I am trying to add .jar to a Python project in Intellij. I added all jars in Project Settings > modules > Dependencies but none of the jar is appearing in external lib. Please suggest if there is any way to attach java .jar to a python project. I tried subprocess.call(['java', '-jar', 'path\\hive-jdbc.jar' ]) resulted in error no main manifest attribute, in hive-jdbc.jar. Please help – Rohit Jun 24 '20 at 19:39
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

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • 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. – David Tonhofer Jul 11 '17 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 '18 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! – Jason Robinson Apr 19 '18 at 04:42
  • Yep. Works well with my IntelliJ as well. Thanks! – Bms bharadwaj Jun 10 '19 at 11:24
  • This works, but, when I reimport gradle dependencies, gradle removes the folder lib. – FabianoLothor Apr 08 '20 at 17:39
  • 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. – – don_Gunner94 Apr 11 '20 at 15:51
  • 2
    @don_Gunner94, via command line, you should also provide the jar like this: `javac -cp ".:/my/path/file.jar;" MyProgram.java` – ROMANIA_engineer Apr 11 '20 at 16:05
  • @ROMANIA_engineer do you mean something like this `javac -cp".:/C:/users/myName/downloads/stdlib.jar;" WorldMap.java` – don_Gunner94 Apr 11 '20 at 16:12
  • The plus button moved to the bottom of the screen now 2021 – Daniel L. VanDenBosch Mar 12 '21 at 18:15
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

user1742529
  • 260
  • 4
  • 16
gurkan
  • 3,457
  • 4
  • 25
  • 38
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 :-)

cesards
  • 15,882
  • 11
  • 70
  • 65
  • 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. – – don_Gunner94 Apr 11 '20 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. – Ariel Mirra Mar 14 '21 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. – Michael Vescovo Mar 25 '21 at 08: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.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Yes, but what if the Maven pom is not called pom.xml? Then it seems like I can't read Maven dependencies. – user1001630 Jul 06 '15 at 02:25
  • 5
    Convention is to call it pom.xml. Follow convention and rename your file. – duffymo Jul 06 '15 at 09: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 '15 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. – vikingsteve May 28 '18 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 06 '18 at 11: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. – – don_Gunner94 Apr 11 '20 at 15:52
  • That’s a packaging or run command issue. You have to tell the JVM how to find JARs like IntelliJ. – duffymo Apr 11 '20 at 16:31
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.

Ali Hesari
  • 1,821
  • 5
  • 25
  • 51
  • Thank you, this was the only thing that worked for me! – Gregordy Apr 16 '18 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. – don_Gunner94 Apr 11 '20 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

Keet Sugathadasa
  • 11,595
  • 6
  • 65
  • 80
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
jumping_monkey
  • 5,941
  • 2
  • 43
  • 58
CuriosCoder
  • 91
  • 1
  • 9
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

Rolland
  • 61
  • 1
  • 1
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

kgui
  • 4,015
  • 5
  • 41
  • 53
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

Arefe
  • 11,321
  • 18
  • 114
  • 168
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

qingmu
  • 402
  • 2
  • 12