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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器