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

2018-12-31 03:51发布

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条回答
皆成旧梦
2楼-- · 2018-12-31 04:14

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
查看更多
笑指拈花
3楼-- · 2018-12-31 04:18

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

查看更多
心情的温度
4楼-- · 2018-12-31 04:23

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.

查看更多
爱死公子算了
5楼-- · 2018-12-31 04:27

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

查看更多
后来的你喜欢了谁
6楼-- · 2018-12-31 04:28

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.

查看更多
何处买醉
7楼-- · 2018-12-31 04: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

查看更多
登录 后发表回答