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

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

Posted by: admin November 2, 2017 Leave a comment

Questions:

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?

Answers:

Steps for adding external jars in IntelliJ IDEA:

  1. Click File from File menu
  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
Questions:
Answers:

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

Questions:
Answers:

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…

Questions:
Answers:

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.

UPDATE: Starting to use Maven to manage dependencies. No need to copy JARs that way – just add them to your pom.xml.

I’m up to IntelliJ 14.1 as of 29-Jan-2015.

Questions:
Answers:

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

Questions:
Answers:

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

Questions:
Answers:

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

Questions:
Answers:

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.