我在MAC上使用netbeans 7.4。在编译以下程序时,我遇到了一些异常。
我的代码:
import java.io.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class HttpClientSample
{
private static String url = "http://www.apache.org/";
public static void main(String[] args)
{
// Constructor for HttpClient
HttpClient client = new HttpClient();
//Creeating a method
GetMethod method = new GetMethod(url);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3,false));
try
{
//Execute the method
int statusCode = client.executeMethod(method);
// System.out.println("vikas is my name ");
if(statusCode != HttpStatus.SC_OK)
{
System.err.println("Method failes : " + method.getStatusLine());
}
//Read the response body
byte[] responsebody = method.getResponseBody();
//Print the response
System.out.println(new String(responsebody));
}
catch (HttpException e)
{
System.out.println("fatal protocol violation: " + e.getMessage());
e.printStackTrace();
}
catch (IOException e )
{
System.out.println("fatal transport error : " + e.getMessage());
e.printStackTrace();
}
finally
{
// release the connection
method.releaseConnection();
}
}
}
运行程序后,我收到以下异常:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.commons.httpclient.HttpClient.<clinit>(HttpClient.java:66)
at JavaApplication1.main(JavaApplication1.java:13)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
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)
任何人都可以帮助我的代码中的问题.. 谢谢你提前..