在我的Java应用程序中使用Apache HttpClient时出现问题。
2019-02-11 07:09:18,270 ERROR [Call-Dequeue-Delegator] (HibernateUtil.java:275) - Building SessionFactory failed.
java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpRequestBase
at java.lang.Class.getDeclaredMethods0(Native Method)
它在我的本地计算机上运行良好,但在服务器上却无法运行。由于它是一个客户端应用程序,所以我没有服务器等的详细信息。
以下是我的代码:
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(20)
.setConnectionRequestTimeout(30).build();
// Creating client with request configuration(timeouts) & disabling redirect following
CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).disableRedirectHandling().build();
String endpoint = this.getSOAPEndPoint();
String queryParamWithSurveyData = addDataToQueryParam(endpoint, customRequestModel);
endpoint += queryParamWithSurveyData;
HttpRequestBase httpRequestWithoutBody = null; // Failing at this
在上面的代码中,它在最后一行失败了,我很奇怪为什么不在第一条代码上,因为这两个(RequestConfig和HttpRequestBase)都是从HTTPClient.jar导入的。
以下是我的POM代码段
//Existing sample dependency
<dependency> //Existing sample dependency
<groupId>com.connectfirst.intelliqueue</groupId>
<artifactId>gson</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/couchbase/gson-2.3.jar</systemPath>
<version>1</version>
</dependency>
// New dependencies added
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpcore-4.4.11.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpclient-4.5.7.jar</systemPath>
<version>1</version>
</dependency>
我在某处看到HttpClient也依赖于其他一些JAR,因此也添加了以下依赖关系,但没有运气。
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpcore-4.4.11.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpclient-4.5.7.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/commons-codec-1.11.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/commons-logging-1.2.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/fluent-hc-4.5.7.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-cache</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpclient-cache-4.5.7.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-osgi</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpclient-osgi-4.5.7.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-win</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpclient-win-4.5.7.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpmime-4.5.7.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/jna-4.5.2.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/jna-platform-4.5.2.jar</systemPath>
<version>1</version>
</dependency>
通过maven全新安装在我的本地上成功创建了JAR。 任何线索都将有所帮助。 谢谢。
答案 0 :(得分:0)
您为什么要选择system
?这将意味着这些jar位于正在运行的应用程序的类路径上。如果将其切换为编译方式,并假设您将其打包为春季启动的JAR或WAR文件,则它们将包含在生成的工件中。否则,应用程序将找不到您正在使用的依赖项。
scope
属性规定了应在类路径上放置依赖项的位置。
test
范围内的依赖项,由于该依赖项不可用,因此代码将无法编译。您可以从official documentation中看到说明。
答案 1 :(得分:0)
请确保您在服务器上使用正确的 Java 版本。
请注意,从 4.4 开始,HttpClient 需要 Java 1.6 或更新版本。对于 4.4 版本之后的 httpClent,您需要使用较新的 java 版本(比如 java 11)。