Eclipse中导入外部jar包
- 在编写java代码时,为方便编程,常常会引用别人已经实现的方法,通常会封装成jar包,我们在编写时,只需引入到Eclipse中即可。
工具/原料
-
Eclipse
-
需要引入的jar包
方法/步骤
-
1
首先在项目下创建一个文件夹,保存我们的jar包。
在项目名上右击,依次点击【New】-->【Floder】,打开新建文件夹窗口
-
2
输入文件夹名称【lib】,点击【ok】。我们通常在lib文件夹中存放从外部引入的jar包
-
3
找到我们要引入的jar包,鼠标选中jar包,然后按住鼠标左键不放,把jar包拖到lib文件夹中。或先复制jar包,然后在lib文件夹上右击,选择复制。此时,打开选择框,我们选择默认的【copy files】,点击【OK】关闭。然后我们就可以在lib文件夹下看到我们复制成功的jar包。
-
4
此时,只是把jar包复制到项目中,还不能使用。我们再在项目名上右击,依次选择
【Build Path】-->【Configure Build Path...】。
-
5
在打开的窗口中,先选中【Libraries】页,再从右边的按钮中点击
【add JARs...】
-
6
在打开的窗口中,我们依次展开本项目的项目和lib文件夹,然后选中我们刚才复制到项目中的jar包,然后点击【OK】关闭窗口
-
7
此时,我们在刚才打开的【Libraries】页中可以看到我们引入的jar包的名称。点击【OK】确认。
-
8
此时,在Eclipse中,我们就可以使用这个jar包了。
END
注意事项
-
当我们不需要该jar包时,只需按相反的顺序即可删除该jar包。
eclipse教程:
25. Using JARs (libraries) in Eclipse
25.1. Adding a Java library to the project classpath
If the libraries should be distributed with your project, you can store the JAR files directly in your project.
For example, you can create a new Java project de.vogella.eclipse.ide.jars
. Then create a new folder called lib
by right-clicking on your project and selecting New → Folder.
From the menu select File → Import → General → File System. Select the Java library you want to import and select the lib
folder as target. Alternatively, just copy and paste the jar
file into the lib
folder.
You can add this library to your classpath, right-click on the JAR file and select Build Path → Add to Build Path.
To manage your classpath, right-click on your project and select Properties. Under Java Build Path → Libraries select the Add JARs button.
The following example shows how the result would look like if the junit-4.4.jar
file had been added to the project.
After adding it to the classpath, Eclipse allows you to use the classes contained in the JAR file in the project . Outside Eclipse you still need to configure your classpath, e.g., via the MANIFEST.MF
file.
25.2. Attach source code to a Java library
You can open any class by positioning the cursor on the class in an editor and pressing F3. Alternatively, you can pressCtrl+Shift+T. This will show a dialog in which you can enter the class name to open it.
If the source code is not available, the editor will show the bytecode of that class.
This happens, for example, if you open a class from a the standard Java library without attaching the source code to it.
To see the source code of such a class, you can attach a source archive or source folder to a Java library. Afterwards, the editor shows the source instead of the bytecode.
Attaching the source code to a library also allows you to debug this source code.
The Source Attachment dialog can be reached in the Java Build Path page of a project. To open this page, right-click on a project and select Properties → Java Build Path. On the Libraries tab, expand the library's node, select the Source Attachment attribute and click the Edit button.
In the Location path field, enter the path of an archive or a folder containing the source.
The following screenshot shows this setting for the standard Java library. If you have the Java Development Kit (JDK) installed, you should find the source in the JDK installation folder. The file is typically called src.zip
.
25.3. Add Javadoc for a JAR
It is also possible to add Javadoc to a library which you use.
Download the Javadoc of the JAR file and put it somewhere in your filesystem.
To enter the location of the Javadoc, open the Java Build Path via a right-click on a project and select Properties → Java Build Path. On the Libraries tab expand the library's node, select the Javadoc location
attribute and press the Edit button.
Enter the location to the file which contains the Javadoc.