xbdarling 2010-06-10 18:51
浏览 527
已采纳

运行myeclipse时出现java.lang.NoClassDefFoundError的错误

我想获得网页的编码,在网上找了可以用chardet.jar中的方法实现,于是下载了它,导入我的工程中,然后用如下代码进行测试:
import info.monitorenter.cpdetector.io.CodepageDetectorProxy;

import info.monitorenter.cpdetector.io.HTMLCodepageDetector;

import info.monitorenter.cpdetector.io.JChardetFacade;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.Set;
public class WebEncoding {
private static CodepageDetectorProxy detector = CodepageDetectorProxy

.getInstance();

static {

detector.add(new HTMLCodepageDetector(false));

detector.add(JChardetFacade.getInstance());

}

/** 测试用例

  • @param args
    */
    public static void main(String[] args) {
    WebEncoding web=new WebEncoding();

try {

System.out.println("*******************");
System.out.println(web.getCharset("http://www.baidu.com/"));

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

  • @param strurl
  • 页面url地址,需要以 http://开始,例:http://www.pujia.com
  • @return
  • @throws IOException
    */
    public String getCharset(String strurl) throws IOException {
    // 定义URL对象
    URL url = new URL(strurl);
    // 获取http连接对象
    HttpURLConnection urlConnection = (HttpURLConnection) url
    .openConnection();
    ;
    urlConnection.connect();
    // 网页编码
    String strencoding = null;

/**

  • 首先根据header信息,判断页面编码
    */
    // map存放的是header信息(url页面的头信息)
    Map> map = urlConnection.getHeaderFields();
    Set keys = map.keySet();
    Iterator iterator = keys.iterator();

// 遍历,查找字符编码

String key = null;

String tmp = null;

while (iterator.hasNext()) {

key = iterator.next();

tmp = map.get(key).toString().toLowerCase();

// 获取content-type charset

if (key != null && key.equals("Content-Type")) {

int m = tmp.indexOf("charset=");

if (m != -1) {

strencoding = tmp.substring(m + 8).replace("]", "");

return strencoding;

}

}

}

/**

  • 通过解析meta得到网页编码
    */
    // 获取网页源码(英文字符和数字不会乱码,所以可以得到正确区域)
    StringBuffer sb = new StringBuffer();
    String line;
    try {
    BufferedReader in = new BufferedReader(new InputStreamReader(url
    .openStream()));
    while ((line = in.readLine()) != null) {
    sb.append(line);
    }
    in.close();
    } catch (Exception e) { // Report any errors that arise
    System.err.println(e);
    System.err
    .println("Usage: java HttpClient []");
    }
    String htmlcode = sb.toString();
    // 解析html源码,取出区域,并取出charset
    String strbegin = "
  • 写回答

4条回答 默认 最新

  • ddzero 2010-06-11 15:42
    关注

    另外关于你用run configurations进行的测试,首先你运行的是
    org.mozilla.intl.chardet.HtmlCharsetDetecto
    这个类,与WebEncoding无关,即是说两者用到的类不一定相同(你的还需要antlr包里的类)。
    其次HtmlCharsetDetecto这个类的main方法是基于参数的,要成功运行当然需要你输入参数了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)