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

14

915

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?

Jaddo answered 26/6, 2009 at 22:58 Comment(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 symbolFoolish
@Foolish You need to understand how the classpath works and that you may have to provide it more than you expect.Hypersensitive
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)"Jehovist
I
1624

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
Inna answered 26/6, 2009 at 23:29 Comment(18)
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.Pander
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...)Torques
@Pander 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'.Corruption
There is no "module" in project settings in the latest version 12.1.4Politico
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.Chlorohydrin
In IJIdea 13, there is "+"> instead of "add".Fretful
IJIdea 13, I got after adding "artifact ...:war exploded: library gson-2 required for module ... is missing from artifact"Moye
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.Jahdal
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...Marrowfat
Where is "Attach Jar Directories"?Nuggar
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.Drank
After adding the dependencies, should we select the export checkbox?Blanketyblank
In my version of Intellij 2017.1.4, there is no "dependencies" tab under modules.Gammadion
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.Daphene
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://mcmap.net/q/54646/-intellij-doesn-39-t-properly-import-jarsFrowsty
If its not being added in this way, check your project's pom.xml file as it may already be there as a <dependency></dependency> tag.Indeliberate
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.Jayjaycee
It still wasn't working for me. Then I discovered that it wasn't ticked. Make sure you. do not miss that.Refractor
P
289

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

Peptide answered 29/9, 2015 at 20:26 Comment(9)
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.Sift
In addition: make sure that file system permissions are set correctly for the .jar files. Use chmod to modify them on Linux or MacOS.Syllabub
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!Guiscard
Yep. Works well with my IntelliJ as well. Thanks!Senility
This works, but, when I reimport gradle dependencies, gradle removes the folder lib.Digraph
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. –Foolish
@don_Gunner94, via command line, you should also provide the jar like this: javac -cp ".:/my/path/file.jar;" MyProgram.javaPeptide
@Peptide do you mean something like this javac -cp".:/C:/users/myName/downloads/stdlib.jar;" WorldMap.javaFoolish
The plus button moved to the bottom of the screen now 2021Aeriela
G
176

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

Grind answered 21/3, 2013 at 9:7 Comment(7)
Simplest and the way we're used to do since time of Eclipse.Suffix
Plain and Simple!!Lipstick
Kind of annoying it still asks for a "name" then, can you just leave any junk in there?Ostiary
This helped me too!Tanguy
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. –Foolish
What does this do under the hood? I mean suppose I want to do the same without IntelliJ.Mallory
@AmirhoseinAl read about classpath, and the -cp option in javaTrincomalee
C
43

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

Capernaum answered 22/2, 2018 at 15:6 Comment(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. –Foolish
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.Costate
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.Diaphaneity
H
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.

Hoyos answered 26/6, 2009 at 23:7 Comment(7)
Yes, but what if the Maven pom is not called pom.xml? Then it seems like I can't read Maven dependencies.Shepley
Convention is to call it pom.xml. Follow convention and rename your file.Hoyos
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?Stutter
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.Tillford
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.Hoyos
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. –Foolish
That’s a packaging or run command issue. You have to tell the JVM how to find JARs like IntelliJ.Hoyos
V
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.

Vaticination answered 24/4, 2017 at 19:40 Comment(2)
Thank you, this was the only thing that worked for me!Barnabe
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.Foolish
E
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

Endoplasm answered 9/11, 2015 at 7:57 Comment(0)
W
7
  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
Wyne answered 8/1, 2021 at 22:41 Comment(0)
B
6

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

Blearyeyed answered 18/2, 2022 at 5:28 Comment(0)
M
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

Musical answered 20/12, 2015 at 13:3 Comment(0)
T
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

Trilbie answered 6/2, 2017 at 22:34 Comment(0)
R
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 ;)

Revels answered 30/4, 2023 at 17:40 Comment(0)
F
0

If you are using Gradle build system for your IntelliJ project, use the following to add local JAR library files.

build.gradle.kts:

dependencies {
    implementation(
        files(
            "$rootDir/myLibs/my-library.jar",
            "another/path/to/library-2.aar",
            // etc.
        )
    )
    // Other dependencies...
}
Fricassee answered 5/1 at 7:48 Comment(0)
P
-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

Passed answered 8/3, 2022 at 11:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.