Java 将外部 jars (lib/*.jar) 添加到 IntelliJ IDEA 项目的正确方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1051640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:47:05  来源:igfitidea点击:

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

javaintellij-idea

提问by knorv

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

在 IntelliJ IDEA 中创建新的 Java 项目时,会创建以下目录和文件:

./projectname.iml
./projectname.ipr
./projectname.iws
./src/

I want to configure IntelliJ IDEA to include my dependency JARs in ./lib/*.jarto the project. What is the correct way to achieve this in IntelliJ IDEA?

我想配置 IntelliJ IDEA 以将我的依赖 JAR 包含./lib/*.jar到项目中。在 IntelliJ IDEA 中实现这一目标的正确方法是什么?

采纳答案by Cambium

Steps for adding external jars in IntelliJ IDEA:

在 IntelliJ IDEA 中添加外部 jar 的步骤:

  1. Click Filefrom the toolbar
  2. Project Structure (CTRL+ SHIFT+ ALT+ Son Windows/Linux, ?+ ;on Mac OS X)
  3. Select Modules at the left panel
  4. Dependencies tab
  5. '+' → JARs or directories
  1. File从工具栏中单击
  2. 项目结构(在 Windows/Linux 上为CTRL+ SHIFT+ ALT+ S,在 Mac OS X 上为?+ ;
  3. 在左侧面板中选择模块
  4. 依赖项选项卡
  5. '+' → JAR 或目录

回答by duffymo

You add them as libraries to your module.

您将它们作为库添加到您的模块中。

I usually have a /libdirectory in my source. I put all the JARs I need there, add /lib as a library, and make it part of my module dependencies.

我通常/lib在我的源中有一个目录。我把我需要的所有 JAR 都放在那里,将 /lib 添加为一个库,并使其成为我的模块依赖项的一部分。

2018 update: I'm using IntelliJ 2017/2018 now.

2018 更新:我现在使用 IntelliJ 2017/2018。

I'm fully committed to Maven and Nexus for dependency management.

我完全致力于 Maven 和 Nexus 的依赖管理。

This is the way the world has gone. Every open source Java project that I know of uses Maven or Gradle. You should, too.

这就是世界的走向。我所知道的每个开源 Java 项目都使用 Maven 或 Gradle。你也应该。

回答by gurkan

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

只需将 .jar 复制粘贴到“libs”文件夹(或整个“libs”文件夹)下,右键单击它并从列表中选择“添加为库”选项。它会做剩下的......

enter image description here

在此处输入图片说明

回答by ROMANIA_engineer

IntelliJ IDEA 15 & 2016

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

  1. 文件>项目结构...

    文件 >项目结构

    或按Ctrl+ Alt+ Shift+S

  2. 项目设置>模块>依赖> " +" 符号 > JAR 或目录...

    模块 >依赖项 >JAR 或目录

  3. 选择jar文件并点击OK,然后点击另一个OK按钮确认

    在此处输入图片说明

    在此处输入图片说明

  4. 可以在“External Libraries”文件夹中查看jar文件

    在此处输入图片说明

回答by Keet Sugathadasa

Libraries cannot be directly used in any program if not properly added to the project gradle files.

如果未正确添加到项目 gradle 文件中,则库不能直接在任何程序中使用。

This can easily be done in smart IDEs like inteli J.

这可以在像 inteli J 这样的智能 IDE 中轻松完成。

1) First as a convention add a folder names 'libs' under your project src file. (this can easily be done using the IDE itself)

1) 首先作为惯例,在您的项目 src 文件下添加一个名为“libs”的文件夹。(这可以使用 IDE 本身轻松完成)

2) then copy or add your library file (eg: .jar file) to the folder named 'libs'

2) 然后将您的库文件(例如:.jar 文件)复制或添加到名为“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.

3) 现在您可以在 libs 文件夹中看到库文件。现在右键单击该文件并选择“添加为库”。这将修复您程序中的所有相关文件,库将直接可供您使用。

Please note:

请注意:

Whenever you are adding libraries to a project, make sure that the project supports the library

每当您向项目添加库时,请确保该项目支持该库

回答by Rolland

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.

在这里找到了一些很好的帮助。但是,尽管正确加载了 JAR,我仍然无法使其工作。后来我发现我不小心在文件结构中创建了模块而不是常规文件夹,并且这个模块是在项目设置中预先选择的。

Here is the footprint:

这是足迹:

File -> Project Structure -> Modules -> (select proper module if you have more) -> Dependencies -> + -> JAR or Libraries

文件 -> 项目结构 -> 模块 ->(如果有更多模块,请选择合适的模块)-> 依赖项 -> + -> JAR 或库

回答by Crt

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 importkeyword followed by the external library's name, continued with dot notation until the desired class is reached.

例如,要访问外部库中的类,您需要使用import关键字,后跟外部库的名称,继续使用点表示法,直到到达所需的类。

Look at the image below to see how I import CodeGenerationExceptionclass from the quickfixj library.

查看下图,了解我如何从 quickfixj 库中导入CodeGenerationException类。

enter image description here

在此处输入图片说明

回答by Ali Hesari

I use this method and it works well:

我使用这种方法并且效果很好:

1- Copy And paste the .jarfiles under the libsfolder.

1-复制并粘贴.jar文件libs夹下的文件。

2- Add compile fileTree(dir: 'libs', include: '*.jar')to dependenciesin build.gradlethen all the jars in the libsfolder will be included..

2- 添加compile fileTree(dir: 'libs', include: '*.jar')dependenciesinbuild.gradle然后libs文件夹中的所有 jars将被包含..

3- Right click on libsfolder and select 'Add as library' option from the list.

3- 右键单击libs文件夹并从列表中选择“添加为库”选项。

回答by cesards

If you are building your project with gradle, you just need to add one line to the dependencies in the build.gradle:

如果你使用 gradle 构建你的项目,你只需要在 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 :-)

然后你把你的罐子放在那里,你很高兴:-)