569

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?

9 답변


983

Steps for adding external jars in IntelliJ IDEA:

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


  • 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
  • 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
  • @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
  • In IJIdea 13, there is "+"> instead of "add". - Val
  • 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

179

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


  • this is in 14 too - ycomp
  • Online Help: jetbrains.com/help/idea/modules.html - David Tonhofer
  • 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
  • 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
  • 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

81

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


  • Simplest and the way we're used to do since time of Eclipse. - Sufian
  • Plain and Simple!! - Anaximandro Andrade
  • Kind of annoying it still asks for a "name" then, can you just leave any junk in there? - dividebyzero
  • This helped me too! - Preeti Joshi

16

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.


  • Yes, but what if the Maven pom is not called pom.xml? Then it seems like I can't read Maven dependencies. - user1001630
  • Convention is to call it pom.xml. Follow convention and rename your file. - duffymo
  • 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
  • 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
  • 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

6

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


4

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.


  • Thank you, this was the only thing that worked for me! - Gregordy

3

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


2

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


2

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

Linked


Related

Latest