如何在 Eclipse 中正确添加外部库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2824515/
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
How to add external library properly in Eclipse?
提问by zihaoyu
So today I downloaded Apache Commons Langlibrary (binary, zip format). I extracted it to C:\eclipse\commons-lang-2.5
folder. There are a commons-lang-2.5.jar
, a commons-lang-2.5-javadoc.jar
, and a commons-lang-2.5-sources.jar
inside, and a folder for HTML Javadoc. I started Eclipse, added commons-lang-2.5.jar
, and set its source and Javadoc respectively as the screenshot below. (http://img43.imageshack.us/img43/9378/eclipsev.png)
所以今天我下载了Apache Commons Lang库(二进制,zip 格式)。我把它解压到C:\eclipse\commons-lang-2.5
文件夹。HTML Javadoc有一个commons-lang-2.5.jar
、一个commons-lang-2.5-javadoc.jar
、一个commons-lang-2.5-sources.jar
inside 和一个文件夹。我启动了 Eclipse,添加了commons-lang-2.5.jar
,并将其源代码和 Javadoc 分别设置为下面的屏幕截图。( http://img43.imageshack.us/img43/9378/eclipsev.png)
My question is, is there a convenient or standard way to add external libraries? Or am I actually doing the right thing?
我的问题是,是否有一种方便或标准的方法来添加外部库?还是我真的在做正确的事情?
回答by Gaurav Vaish
Recommendation:
推荐:
- Create a "lib" folder and keep all your jars in the folder.
- Subsequently, add all the jar files in the lib folder into your build path by using
Project => Properties => Java Build Path => Libraries => Add JAR ...
- 创建一个“lib”文件夹并将所有 jars 保存在该文件夹中。
- 随后,使用以下命令将 lib 文件夹中的所有 jar 文件添加到您的构建路径中
Project => Properties => Java Build Path => Libraries => Add JAR ...
btw, there' no screenshot. Can you give the link for the screen shot so that I may be able to help our better...
顺便说一句,没有截图。您能否提供屏幕截图的链接,以便我可以帮助我们更好地...
回答by parsa
You must add jar file on lib folder and then right click on jar file and click "build path"-->add to build path and now you can write that jar file code
您必须在 lib 文件夹中添加 jar 文件,然后右键单击 jar 文件并单击“构建路径”--> 添加到构建路径,现在您可以编写该 jar 文件代码
回答by Jossef Harush
Use maven
用 maven
You don'thave to download all jar'sinto a folder by yourself! - use maven. It's based on a public repository, and you manage your dependencies in an xml file.
你不必须下载所有jar的放到一个文件夹自己!- 使用 Maven。它基于公共存储库,您可以在 xml 文件中管理您的依赖项。
Your project will have a pom.xml
file that going to look like this:
您的项目将有一个如下所示的pom.xml
文件:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-module</artifactId>
<version>1</version>
</project>
and in this file you manage the external library dependencies
并在此文件中管理外部库依赖项
for instance, if you wish to add this dependency - http://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.4
例如,如果您希望添加此依赖项 - http://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.4
you will need to modify your pom.xml
like so:
你需要pom.xml
像这样修改你的喜欢:
<project>
...
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
...
</project>
and save it. that's it
并保存它。就是这样
- Read this getting started tutorial - https://maven.apache.org/guides/getting-started/
- eclipse plugin - https://maven.apache.org/plugins/maven-eclipse-plugin
回答by SH'
As the question is quite general, maybe following detailed answer could help others:
由于问题很笼统,也许以下详细答案可以帮助其他人:
- Right click on your project name
- Select and expand the correct (e.g C/C++) compiler
- Go to "Includes"
- On right hand corner, you would see a small icon to "add path" to include your custom library
- Add, build and happy compiling
- 右键单击您的项目名称
- 选择并展开正确的(例如 C/C++)编译器
- 转到“包括”
- 在右上角,您会看到一个小图标来“添加路径”以包含您的自定义库
- 添加、构建和愉快的编译