简体   繁体   English

如何在Java中处理两个jar文件?

[英]How to handle two jar files in java?

I am using Netbeans IDE for a java project. 我正在将Netbeans IDE用于Java项目。 In this project i need a jar file "htmlunit-2.6.jar". 在这个项目中,我需要一个jar文件“ htmlunit-2.6.jar”。

I have included this jar file in the project libraries folder. 我已将此jar文件包含在项目库文件夹中。 I have instantiated one of its class "WebClient" but this class needs other classes of "commons-httpclient-3.1.jar" file. 我已经实例化了其类之一“ WebClient”,但该类需要“ commons-httpclient-3.1.jar”文件的其他类。

Now I have also included "commons-httpclient-3.1.jar" file in the project libraries folder. 现在,我还在项目库文件夹中包含了“ commons-httpclient-3.1.jar”文件。 But when I compiled my source file, it throws 但是当我编译源文件时,它会抛出

ClassNotFoundException: org.apache.commons.httpclient.auth.CredentialsProvider ClassNotFoundException:org.apache.commons.httpclient.auth.CredentialsProvider

Kindly tell me how to handle this situation when one class in one jar file needs other classes in other jar file. 请告诉我当一个jar文件中的一个类需要另一个jar文件中的其他类时如何处理这种情况。

Simply put the required jar files on the classpath at compile-time and it should work. 只需在编译时将所需的jar文件放在类路径上即可。 If you were doing it from the command-line then it would look like this: 如果您是从命令行执行的,则如下所示:

javac -cp jar1:jar2 my.Application

If you are using NetBeans then you need to tell NetBeans that both of the JARs are on your classpath. 如果使用的是NetBeans则需要告诉NetBeans这两个JAR都在类路径中。 It will be definable in a Project > Properties wizard as described here and also here from the tutorial 这将是自定义的项目>属性向导这里描述 ,并从这里教程

The ClassNotFoundException tells you that your libraries have some dependencies that you don't have included in your classpath at runtime. ClassNotFoundException告诉您,您的库具有一些运行时未包含在类路径中的依赖项。 Your source is OK, because if you have used something not available, NB will tell you this at compile time (or before when editing). 您的消息来源还可以,因为如果您使用的内容不可用,NB将在编译时(或在编辑之前)告诉您。

So, welcome in the " dependency hell " of Java. 因此,欢迎来到Java的“ 依赖地狱 ”。 For small projects you will be able to check all dependencies by hand with readme files, docs, etc and put them in the project config as oxbow_lakes said. 对于小型项目,您将可以使用自述文件,文档等手动检查所有依赖项,并按照oxbow_lakes的说明将它们放在项目配置中。 For bigger things look at maven . 对于更大的事情,请看maven It will do (most) everything for you ! 它将为您做(大部分)一切!

(Maven is available in NB6) (Maven在NB6中可用)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM