Java 如何在 Eclipse 中导入 jar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3280353/
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 import a jar in Eclipse
提问by user395734
How do I import a jar in Eclipse?
如何在 Eclipse 中导入 jar?
采纳答案by bragboy
You can add a jar in Eclipse by right-clicking on the Project → Build Path → Configure Build Path. Under Libraries tab, click Add Jars or Add External JARs and give the Jar. A quick demo here.
您可以通过右键单击“项目”→“构建路径”→“配置构建路径”在 Eclipse 中添加 jar。在 Libraries 选项卡下,单击 Add Jars 或 Add External JARs 并提供 Jar。这里有一个快速演示。
The above solution is obviously a "Quick" one. However, if you are working on a project where you need to commit files to the source control repository, I would recommend adding Jar files to a dedicated library folder within your source control repository and referencing few or all of them as mentioned above.
上述解决方案显然是一个“快速”的解决方案。但是,如果您正在处理需要将文件提交到源代码控制存储库的项目,我建议将 Jar 文件添加到源代码控制存储库中的专用库文件夹中,并如上所述引用其中的一部分或全部。
回答by Ashraf Bashir
Here are the steps:
以下是步骤:
click File > Import. The Import window opens.
Under Select an import source, click J2EE > App Client JAR file.
Click Next.
In the Application Client file field, enter the location and name of the application client JAR file that you want to import. You can click the Browse button to select the JAR file from the file system.
In the Application Client project field, type a new project name or select an application client project from the drop-down list. If you type a new name in this field, the application client project will be created based on the version of the application client JAR file, and it will use the default location.
In the Target runtime drop-down list, select the application server that you want to target for your development. This selection affects the run time settings by modifying the class path entries for the project.
If you want to add the new module to an enterprise application project, select the Add project to an EAR check box and then select an existing enterprise application project from the list or create a new one by clicking New.
Note: If you type a new enterprise application project name, the enterprise application project will be created in the default location with the lowest compatible J2EE version based on the version of the project being created. If you want to specify a different version or a different location for the enterprise application, you must use the New Enterprise Application Project wizard.
Click Finish to import the application client JAR file.
单击文件 > 导入。导入窗口打开。
在选择导入源下,单击 J2EE > App Client JAR 文件。
点击下一步。
在应用程序客户端文件字段中,输入要导入的应用程序客户端 JAR 文件的位置和名称。您可以单击浏览按钮从文件系统中选择 JAR 文件。
在应用程序客户端项目字段中,输入新项目名称或从下拉列表中选择应用程序客户端项目。如果您在此字段中键入新名称,应用程序客户端项目将根据应用程序客户端 JAR 文件的版本创建,并且将使用默认位置。
在目标运行时下拉列表中,选择您要作为开发目标的应用程序服务器。此选择通过修改项目的类路径条目来影响运行时设置。
如果要将新模块添加到企业应用程序项目,请选中将项目添加到 EAR 复选框,然后从列表中选择现有的企业应用程序项目或通过单击新建创建一个新的。
注意:如果您键入一个新的企业应用程序项目名称,则企业应用程序项目将在默认位置创建,该位置具有基于所创建项目版本的最低兼容 J2EE 版本。如果要为企业应用程序指定不同的版本或不同的位置,则必须使用“新建企业应用程序项目”向导。
单击完成以导入应用程序客户端 JAR 文件。
回答by VonC
Two choices:
两种选择:
1/ From the project:
1/来自项目:
2/ If you have already other jar imported, from the directory "References Libraries":
2/如果您已经导入了其他jar,请从“参考库”目录中:
Both will lead you to this screen where you can mange your libraries:
两者都将引导您进入此屏幕,您可以在其中管理您的库:
回答by Frank Grimm
Just a comment on importing jars into Eclipse (plug-in development) projects:
简单评论一下将 jars 导入 Eclipse(插件开发)项目:
In case you are developing Eclipse plug-ins, it makes sense to use Eclipse's native bundling mechanism instead of just importing the jar into a plug-in project. Eclipse (or better its underlying OSGi runtime, Equinox) uses so-called bundles which contain some more information than plain jars (e.g., version infos, dependencies to other bundles, exported packages; see the MANIFEST.MF file). Because of this information, OSGi bundles can be dynamically loaded/unloaded and there is automatic dependency resolution available in an OSGi/Eclipse runtime. Hence, using OSGi bundles instead of plain jars (contained inside another OSGi bundle) has some advantages.
如果您正在开发 Eclipse 插件,那么使用 Eclipse 的本机捆绑机制而不是仅仅将 jar 导入插件项目是有意义的。Eclipse(或者更好的是它的底层 OSGi 运行时,Equinox)使用所谓的包,它包含比普通 jar 包更多的信息(例如,版本信息、对其他包的依赖、导出的包;参见 MANIFEST.MF 文件)。由于这些信息,OSGi 包可以动态加载/卸载,并且在 OSGi/Eclipse 运行时中可以使用自动依赖项解析。因此,使用 OSGi 包而不是普通的 jar(包含在另一个 OSGi 包中)有一些优势。
(BTW: Eclipse plug-ins are the same thing as OSGi bundles.)
(顺便说一句:Eclipse 插件与 OSGi 包是一回事。)
There is a good chance that somebody already bundled a certain (3rd party) library as an OSGi bundle. You might want to take a look at the following bundle repositories:
很有可能有人已经将某个(第 3 方)库捆绑为 OSGi 捆绑包。您可能想查看以下捆绑存储库:
回答by Hesham Yassin
Adding external Jar is not smart in case you want to change the project location in filesystem.
如果您想更改文件系统中的项目位置,添加外部 Jar 并不明智。
The best way is to add the jar to build path so your project will compile if exported:
最好的方法是将 jar 添加到构建路径,以便您的项目在导出时编译:
Create a folder called lib in your project folder.
copy to this folder all the jar files you need.
Refresh your project in eclipse.
Select all the jar files, then right click on one of them and select Build Path -> Add to Build Path
在您的项目文件夹中创建一个名为 lib 的文件夹。
将您需要的所有 jar 文件复制到此文件夹。
在 Eclipse 中刷新您的项目。
选择所有 jar 文件,然后右键单击其中之一并选择 Build Path -> Add to Build Path
回答by Hesham Yassin
Eclipse -> Preferences -> Java -> Build Path -> User Libraries -> New(Name it) -> Add external Jars
Eclipse -> Preferences -> Java -> Build Path -> User Libraries -> New(Name it) -> Add external Jars
(I recommend dragging your new libraries into the eclipse folder before any of these steps to keep everything together, that way if you reinstall Eclipse or your OS you won't have to rwlink anything except the JDK) Now select the jar files you want. Click OK.
(我建议在执行任何这些步骤之前将您的新库拖到 eclipse 文件夹中以将所有内容保持在一起,这样如果您重新安装 Eclipse 或您的操作系统,除了 JDK 之外,您将不必 rwlink 任何东西)现在选择您想要的 jar 文件。单击确定。
Right click on your project and choose Build Path -> Add Library
右键单击您的项目并选择 Build Path -> Add Library
FYI just code and then right click and Source->Organize Imports
仅供参考,只需编写代码,然后右键单击并单击 Source->Organize Imports
回答by amigana _34
first of all you will go to your project what you are created and next right click in your mouse and select properties in the bottom and select build in path in the left corner and add external jar file add click apply .that's it
首先,您将转到您创建的项目,然后右键单击鼠标并选择底部的属性,然后选择左角的构建路径并添加外部 jar 文件添加单击应用。就是这样
回答by Manideep
Jar File in the system path is:
系统路径中的Jar文件为:
C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.jar
C:\oraclex\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.jar
ojdbc14.jar(it's jar file)
ojdbc14.jar(它是jar文件)
To import jar file in your Eclipse IDE, follow the steps given below.
要在 Eclipse IDE 中导入 jar 文件,请按照以下步骤操作。
- Right-click on your project
- Select Build Path
- Click on Configure Build Path
- Click on Libraries and select Add External JARs
- Select the jar file from the required folder
- Click and Apply and Ok
- 右键单击您的项目
- 选择构建路径
- 单击配置构建路径
- 单击库并选择添加外部 JAR
- 从所需文件夹中选择 jar 文件
- 单击并应用和确定
回答by Datta
In eclipse I included a compressed jar file i.e. zip file. Eclipse allowed me to add this zip file as an external jar but when I tried to access the classes in the jar they weren't showing up.
在 Eclipse 中,我包含了一个压缩的 jar 文件,即 zip 文件。Eclipse 允许我将此 zip 文件添加为外部 jar,但是当我尝试访问 jar 中的类时,它们没有出现。
After a lot of trial and error I found that using a zip format doesn't work. When I added a jar file then it worked for me.
经过大量试验和错误,我发现使用 zip 格式不起作用。当我添加一个 jar 文件时,它对我有用。