PhoenixPerson
PhoenixPerson

Reputation: 312

Unable to run a simple HTTP client in java

In the directory of my code, there are following files:

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

and a java source file:

JSONLoader.java

I execute the following commands in the same directory:

javac -cp *;. JSONLoader.java

and

java -cp "*;." JSONLoader

I get the following error after the second command:

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

Here is the java code file:

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();
          }

    }
}

Am I missing something?

Upvotes: 1

Views: 2632

Answers (5)

Lajos Arpad
Lajos Arpad

Reputation: 77002

java.lang.NoClassDefFoundError means that ummmm... No Class Def Found Error occurred. Your code is trying to use a class somewhere but it does not exist. In your case, the class is

org/apache/commons/logging/LogFactory

So I have typed the following text into a search engine:

download org/apache/commons/logging/LogFactory

and the first result was this.

Download it and add to your project.

Upvotes: 0

SMA
SMA

Reputation: 37083

Basically you get ClassNotFoundException when you dont have corresponding classes at runtime. Here you are missing apache's logging and codec jar on which apache-httpclient depends upon. see dependencies that you need.

So once you download them, add them to your classpath or in your -cp command line along with httpcore jar and rerun the code.

Upvotes: 1

Ahmad MOUSSA
Ahmad MOUSSA

Reputation: 2916

There are some missed jar files (commons-logging.jar), you can download it from here

I hope it will help you, best regards.

Upvotes: 0

AlexR
AlexR

Reputation: 115388

Please examine this line from the stack trace:

 Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory

This means that apache commons logging (dependency of http client) is missing. And it is indeed missing in your command line.

You have to add there all dependencies including depnedencies of your third party and their third parties.

Upvotes: 2

Jens
Jens

Reputation: 69480

Download commons-logging.jar and ad it to your classpath.

Upvotes: 2

Related Questions