Figo1212 2013-12-04 15:02 采纳率: 0%
浏览 3034

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

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条回答 默认 最新

  • reed2014 2014-08-21 08:58
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。