gpt4 book ai didi

java - 无法在 Java 中运行简单的 HTTP 客户端

转载 作者:行者123 更新时间:2023-11-29 07:43:54 24 4
gpt4 key购买 nike

在我的代码目录下,有以下文件:

apache-httpcomponents-httpcore.jar
httpclient-4.0.3.jar
java-json.jar

和一个java源文件:

JSONLoader.java

我在同一目录下执行以下命令:

javac -cp *;. JSONLoader.java

java -cp "*;." JSONLoader

我在第二个命令后收到以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:159)
at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:178)
at JSONLoader.main(JSONLoader.java:20)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more

这是java代码文件:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JSONLoader {

/**
* @param args
*/
public static void main(String[] args) {
try {

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(
"http://www.espncricinfo.com/netstorage/754739.json");
getRequest.addHeader("accept", "application/json");

HttpResponse response = httpClient.execute(getRequest);

if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatusLine().getStatusCode());
}

BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));

String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}

httpClient.getConnectionManager().shutdown();

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();
}

}
}

我错过了什么吗?

最佳答案

下载 commons-logging.jar 并将其添加到您的类路径中。

关于java - 无法在 Java 中运行简单的 HTTP 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27525427/

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