如何在eclipse中使用JAR中的一个类 [英] How to use a class from JAR in eclipse

查看:228
本文介绍了如何在eclipse中使用JAR中的一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个jar文件 - jar1和jar2。它们都位于C:\Eclipse projects\中,我已经将路径添加到环境变量CLASSPATH中,如下所示

 ; C:\Program Files\Java\jre6\lib\ext\QTJava.zip; C:\Eclipse projects\stdlib.jar; C:\Eclipse projects\\ \\ algs4.jar 

。在那里,我离开了他们。然后我将罐子从他们的位置C:\Eclipse projects\添加到项目中,并显示为参考库。然而,当我尝试从罐子中实例化一个类时,它不会识别它。我也无法导入jar(import jar1)。



在我尝试在项目中添加一个lib文件夹后,我添加了jar。之后,我再次将它们添加为引用(所以不会在引用库中出现两次),但是我仍然无法使用内部类。任何帮助将不胜感激。



更新:
我的结局一定是错误的。没有一个建议对我有用。这是一个包含所有步骤的视频:screencast.com/t/gC81YzCsLY0e



解决方案在我的项目中,我有一个名为TestProject的包,它似乎这些jar需要一个默认包。删除TestProject包并使用defaultPackage后,如下所述添加外部JAR后,所有内容都正常工作。

解决方案

在eclipse中,右键单击在项目 - > Propeties-> Java构建路径 - >添加外部JAR(如果jar在项目的文件夹内,则添加JAR),然后选择您的jar文件。
从现在起,你可以使用你添加的jar的内部类。当您开始使用Eclipse时,Eclipse将导入它们。


I have two jar files - jar1 and jar2. Both of them are located in C:\Eclipse projects\ and I have added the paths to both of them to the Environment Variable CLASSPATH as follows

.;C:\Program Files\Java\jre6\lib\ext\QTJava.zip;C:\Eclipse projects\stdlib.jar;C:\Eclipse projects\algs4.jar

the ".;" at the beginning were there so I left them. Then I added the jars to the project from their location C:\Eclipse projects\ and they showed up as Referenced Libraries. However, when I try to instantiate a class from the jars it does not recognize it. I am also not able to import the jar (import jar1).

After I tried adding a lib folder in the project and I added the jars there. After, I added them as references once again (so not they appear twice in the Referenced Libraries), however, I am still not able to use the inner classes. Any help will be much appreciated.

UPDATE: Something must be wrong on my end. None of the suggestions worked for me. Here is a video with all the steps: screencast.com/t/gC81YzCsLY0e

RESOLUTION In my project I had a package called TestProject and it seems that those jars needed a default package. After deleting the TestProject package and using a defaultPackage everything worked correctly after adding external JARs as explained below.

解决方案

In eclipse, right click on a project->Propeties->Java Build Path->Add External JARs (Add JARs if the jar is inside the project's folder) and then choose your jar file. From now you can use the inner classes of the jars you added. Eclipse will import them when you'll start using them.

这篇关于如何在eclipse中使用JAR中的一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆