2 xiaoxiami365 xiaoxiami365 于 2013.12.04 23:02 提问

<自己动手写网络爬虫>时遇到的报错

package com.guet.crawlerbyself;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

public class RetrivePage
{

private static HttpClient httpClient = new HttpClient();
//set a proxy server

static 
{
    //set IP Address and the port of the proxy server
    httpClient.getHostConfiguration().setProxy("localhost",8080);
}

public static boolean downloadPage(String path) throws HttpException, IOException 
{
    InputStream input = null;
    OutputStream output = null;

    //get post method
    PostMethod postMethod = new PostMethod("www.baidu.com");

    //set parameters of post method
    NameValuePair[] postData = new NameValuePair[2];
    postData[0] = new NameValuePair("name","baidu");
    postData[1] = new NameValuePair("password","123");

    //Adds an array of parameters to be used in the POST request body.
    postMethod.addParameters(postData);

    //execute and return status code.
    int statusCode = httpClient.executeMethod(postMethod);

    //this place,only process the status 200.
    if(statusCode == HttpStatus.SC_OK)
    {
        input = postMethod.getResponseBodyAsStream();
        String filename = path.substring(path.lastIndexOf('/')+1);
        output = new FileOutputStream(filename);

        //output to the file
        int tempByte = -1;
        while((tempByte = input.read())>0){
            output.write(tempByte);
        }

        if (input != null ){
            input.close();
        }
        if(output != null){
            output.close();
        }
        return true;
    }

    return false;
}

public static void main(String[] args)
{
    System.out.println("execute");
    try{
        RetrivePage.downloadPage("http://www.baidu.com/");
    }catch(HttpException e){
        System.out.println("httpexeption");
        e.printStackTrace();
    }catch(IOException e){
        System.out.println("ioexeption");
        e.printStackTrace();
    }
}

}
运行时弹出对话框显示:could not find the main class. program will exit
console中显示:
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.commons.httpclient.HttpClient.(HttpClient.java:66)
at com.guet.crawlerbyself.RetrivePage.(RetrivePage.java:17)
Exception in thread "main"

所需的apache的开源包我已经导入了,代码写完后也没报错,运行的时候就报错了.
我在网上google了一下,有的说把jdk改成1.4的之类的答案,我试过了,还是老问题没什么效果.希望之前碰到这个问题的朋友帮忙解答一下.

3个回答

u010631928
u010631928   2014.08.21 16:58

首先这个问题,我最近正好也遇到,你的代码我没有仔细看,不过从出错情况来看是缺少相应的包:commons-loggingxx.jar,之后如果还有错误,百度一下即可

Darry720
Darry720   2015.04.19 14:07

为什么安装了commons-loggingxx。jar之后,还有其他问题

ALT_LB
ALT_LB   2016.11.28 17:57

是的 一直是这个问题

Csdn user default icon
上传中...
上传图片
插入图片