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

后端 未结 9 1497
时光取名叫无心
时光取名叫无心 2020-11-21 10:20

When creating a new Java project in IntelliJ IDEA, the following directories and files are created:

./projectname.iml
./projectname.ipr
./projectname.iws
./s         


        
相关标签:
9条回答
  • 2020-11-21 10:29

    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.

    0 讨论(0)
  • 2020-11-21 10:29

    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

    0 讨论(0)
  • 2020-11-21 10:32

    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.

    0 讨论(0)
  • 2020-11-21 10:34

    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...

    0 讨论(0)
  • 2020-11-21 10:35

    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:

    Then you drop your jars in there and you are good to go :-)

    0 讨论(0)
  • 2020-11-21 10:39

    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.

    0 讨论(0)
提交回复
热议问题