2014-09-24 136 views
1

我将Apache HttpClient 4.3.5 jar添加到我的项目中的新用户库,但是每当我运行我的程序时,都会收到NoClassDefFound运行时错误。我可以告诉这是由HttpClient类引起的。我不知道如何缓解这个问题。将HttpClient jar添加到eclipse

+0

你通过'Add external jars'添加了罐子? – venkatKA 2014-09-24 04:19:57

+0

你如何准确地运行你的程序?在Eclipse中用'main()'方法右键单击一个类并选择'Run'?或者在命令行上? – Asaph 2014-09-24 04:20:31

+0

如果你使用Eclipse,右键单击项目,Build path-> Add external jar,选择你httpclient jar并按ok! – Vito 2014-09-24 09:04:24

回答

1

NoClassDefFound通常表示您缺少对类路径的所需依赖关系。

如果你看看Maven pom.xml for httpclient,你会发现它有一些传递性依赖关系,它的含义取决于其他工件。

<dependency> 
    <groupId>org.apache.httpcomponents</groupId> 
    <artifactId>httpcore</artifactId> 
</dependency> 
<dependency> 
    <groupId>commons-logging</groupId> 
    <artifactId>commons-logging</artifactId> 
</dependency> 
<dependency> 
    <groupId>commons-codec</groupId> 
    <artifactId>commons-codec</artifactId> 
</dependency> 

所以你可以看到三个神器httpclient取决于:httpcorecommons-loggingcommons-codec

这就是说,你可能会错过这些依赖关系(因此NoClassDefFound)。如果您使用的是Maven,当您将httpclient作为依赖项添加时,Maven会为您提供这些传递依赖项int。

但是,它看起来并不像您使用的是Maven。所以你想要做的是下载整个包在HttpComponents Home Page。如果你抓住了二进制distrubutions,像4.3.5.zip,并解压缩,你会看到在lib DIR所有这些罐子:做的仅仅是添加所有这些罐子到一个库

commons-codec-1.6 
commons-logging-1.1.3 
fluent-hc-4.3.5 
httpclient-4.3.5 
httpclient-cache-4.3.5 
httpcore-4.3.2 
httpmime-4.3.5 

最好的事情。然后将所有该库添加到您的项目。

  1. 简单转到[窗口] → [设置] →【JAVA] → [构建路径] → [用户库]
  2. 选择并键入一个名称
  3. 选择新的图书馆,选择添加外部罐子,浏览并添加您下载的lib目录中的所有罐子。
  4. 将库添加到您的项目中。