gpt4 book ai didi

java - 无法使用 Gradle HttpClient 依赖项运行 Jar

转载 作者:行者123 更新时间:2023-12-01 17:58:36 25 4
gpt4 key购买 nike

在 IntelliJ 中使用 gradle 构建后,我尝试运行 jar 文件,但出现以下错误:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpUriRequest
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpUriRequest
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

我的 build.gradle 看起来像这样:

dependencies {
compile 'org.apache.httpcomponents:httpclient:4.5.3'

testCompile group: 'junit', name: 'junit', version: '4.11'
}

我已经在 IntelliJ 的模块设置中检查了 httpcomponents 和 commons-codec 的依赖关系。

源代码:

package com.jshah;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import static org.apache.http.HttpHeaders.USER_AGENT;


public class Runner {
public static void main(String[] args) {
final String url = "http://reddit.com/api/trending_subreddits";

try {
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);

request.addHeader("User-Agent", USER_AGENT);
HttpResponse response = client.execute(request);

System.out.println(response.getStatusLine());

// Get the response
BufferedReader rd = new BufferedReader
(new InputStreamReader(
response.getEntity().getContent()));

String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
}
catch (Exception e) {

}
}
}

最佳答案

你是否只是通过调用:gradle build来构建你的项目(不添加任何额外的插件,除了java)?如果是这样,则您的 jar 中没有 httpclient 类。您需要使用 FatJarCopy dependencies构建您的项目。

关于java - 无法使用 Gradle HttpClient 依赖项运行 Jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42689510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com