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
Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project
提问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/*.jar
to 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 的步骤:
- Click
File
from the toolbar - Project Structure (CTRL+ SHIFT+ ALT+ Son Windows/Linux, ?+ ;on Mac OS X)
- Select Modules at the left panel
- Dependencies tab
- '+' → JARs or directories
File
从工具栏中单击- 项目结构(在 Windows/Linux 上为CTRL+ SHIFT+ ALT+ S,在 Mac OS X 上为?+ ;)
- 在左侧面板中选择模块
- 依赖项选项卡
- '+' → JAR 或目录
回答by duffymo
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.
我通常/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
回答by ROMANIA_engineer
IntelliJ IDEA 15 & 2016
IntelliJ IDEA 15 & 2016
File> Project Structure...
or press Ctrl+ Alt+ Shift+ S
Project Settings> Modules> Dependencies> "+" sign > JARs or directories...
Select the jar file and click on OK, then click on another OK button to confirm
You can view the jar file in the "External Libraries" folder
文件>项目结构...
或按Ctrl+ Alt+ Shift+S
项目设置>模块>依赖> " +" 符号 > JAR 或目录...
选择jar文件并点击OK,然后点击另一个OK按钮确认
可以在“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类。
回答by Ali Hesari
I use this method and it works well:
我使用这种方法并且效果很好:
1- Copy And paste the .jar
files under the libs
folder.
1-复制并粘贴.jar
文件libs
夹下的文件。
2- Add compile fileTree(dir: 'libs', include: '*.jar')
to dependencies
in build.gradle
then all the jars in the libs
folder will be included..
2- 添加compile fileTree(dir: 'libs', include: '*.jar')
到dependencies
inbuild.gradle
然后libs
文件夹中的所有 jars将被包含..
3- Right click on libs
folder 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:
然后将该文件夹添加到您的根项目或模块:
Then you drop your jars in there and you are good to go :-)
然后你把你的罐子放在那里,你很高兴:-)