0
我想從Java做一個簡單的JSON的客戶端。 所以我建立這個代碼:的HttpClient的Java NoClassDefFoundError的
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonSimple {
private static final String FILENAME = "C:\\Users\\a41g.txt";
public static void main(String[] args) {
JsonSimple s = new JsonSimple();
s.http();
}
public HttpResponse http() {
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(FILENAME);
} catch (FileNotFoundException e1) {
}
br = new BufferedReader(fr);
String sCurrentLine;
try {
br = new BufferedReader(new FileReader(FILENAME));
} catch (FileNotFoundException e1) {
}
try {
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { //THIS LINE GENERATED EXCEPTION
HttpPost request = new HttpPost("http://mylik");
try {
JSONObject json = new JSONObject(sCurrentLine);
StringEntity se = new StringEntity(json.toString());
request.addHeader("content-type", "application/json");
request.setEntity(se);
HttpResponse response = httpClient.execute(request);
if (response != null) {
InputStream in = response.getEntity().getContent(); //Get the data in the entity
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}
}
,但如果我想開始我的應用我有這樣的錯誤:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/config/Lookup
at JsonSimple.http(JsonSimple.java:47)
at JsonSimple.main(JsonSimple.java:20)
Caused by: java.lang.ClassNotFoundException: org.apache.http.config.Lookup
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 2 more
我插在我的附近產生的行代碼代碼中的註釋例外。
EDIT this is the jar that I have imported in my project
EDIT (I have change .jar file) in this:
看來你沒有包括所有jar文件 –
添加此庫,而不是 http://www.java2s.com/Code/Jar/h /Downloadhttpclient43beta1jar.htm –
可能是您在此鏈接中的答案。 https://stackoverflow.com/questions/36746057/exception-in-thread-main-java-lang-noclassdeffounderror-org-apache-http-conne – Vasant