当前位置: 首页 > 知识库问答 >
问题:

线程“main”java.lang.noClassDefFoundError:org/jsoup/jsoup中出现异常

林祯
2023-03-14
Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/Jsoup
    at com.copiedcrawler.SpiderLeg.crawl(SpiderLeg.java:35)
    at com.copiedcrawler.Spider.search(Spider.java:40)
    at com.copiedcrawler.SpiderTest.main(SpiderTest.java:9)
Caused by: java.lang.ClassNotFoundException: org.jsoup.Jsoup
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 3 more
package com.copiedcrawler;


import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class Spider
{
  private static final int MAX_PAGES_TO_SEARCH = 10;
  private Set<String> pagesVisited = new HashSet<String>();
  private List<String> pagesToVisit = new LinkedList<String>();

  public void search(String url, String searchWord)
  {
      while(this.pagesVisited.size() < MAX_PAGES_TO_SEARCH)
      {
          String currentUrl;
          SpiderLeg leg = new SpiderLeg();
          if(this.pagesToVisit.isEmpty())
          {
              currentUrl = url;
              this.pagesVisited.add(url);
          }
          else
          {
              currentUrl = this.nextUrl();
          }
          leg.crawl(currentUrl); // Lots of stuff happening here. Look at the crawl method in
                                 // SpiderLeg
          boolean success = leg.searchForWord(searchWord);
          if(success)
          {
              System.out.println(String.format("**Success** Word %s found at %s", searchWord, currentUrl));
              break;
          }
          this.pagesToVisit.addAll(leg.getLinks());
      }
      System.out.println("\n**Done** Visited " + this.pagesVisited.size() + " web page(s)");
  }


  /**
   * Returns the next URL to visit (in the order that they were found). We also do a check to make
   * sure this method doesn't return a URL that has already been visited.
   * 
   * @return
   */
  private String nextUrl()
  {
      String nextUrl;
      do
      {
          nextUrl = this.pagesToVisit.remove(0);
      } while(this.pagesVisited.contains(nextUrl));
      this.pagesVisited.add(nextUrl);
      return nextUrl;
  }
}
package com.copiedcrawler;


import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class SpiderLeg
{
    // We'll use a fake USER_AGENT so the web server thinks the robot is a normal web browser.
    private static final String USER_AGENT =
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1";
    private List<String> links = new LinkedList<String>();
    private Document htmlDocument;


    /**
     * This performs all the work. It makes an HTTP request, checks the response, and then gathers
     * up all the links on the page. Perform a searchForWord after the successful crawl
     * 
     * @param url
     *            - The URL to visit
     * @return whether or not the crawl was successful
     */
    public boolean crawl(String url)
    {
        try
        {
            Connection connection = Jsoup.connect(url).userAgent(USER_AGENT);
            Document htmlDocument = connection.get();
            this.htmlDocument = htmlDocument;
            if(connection.response().statusCode() == 200) // 200 is the HTTP OK status code
                                                          // indicating that everything is great.
            {
                System.out.println("\n**Visiting** Received web page at " + url);
            }
            if(!connection.response().contentType().contains("text/html"))
            {
                System.out.println("**Failure** Retrieved something other than HTML");
                return false;
            }
            Elements linksOnPage = htmlDocument.select("a[href]");
            System.out.println("Found (" + linksOnPage.size() + ") links");
            for(Element link : linksOnPage)
            {
                this.links.add(link.absUrl("href"));
            }
            return true;
        }
        catch(IOException ioe)
        {
            // We were not successful in our HTTP request
            return false;
        }
    }


    /**
     * Performs a search on the body of on the HTML document that is retrieved. This method should
     * only be called after a successful crawl.
     * 
     * @param searchWord
     *            - The word or string to look for
     * @return whether or not the word was found
     */
    public boolean searchForWord(String searchWord)
    {
        // Defensive coding. This method should only be used after a successful crawl.
        if(this.htmlDocument == null)
        {
            System.out.println("ERROR! Call crawl() before performing analysis on the document");
            return false;
        }
        System.out.println("Searching for the word " + searchWord + "...");
        String bodyText = this.htmlDocument.body().text();
        return bodyText.toLowerCase().contains(searchWord.toLowerCase());
    }


    public List<String> getLinks()
    {
        return this.links;
    }

}

包com.copiedcrawler;

public class SpiderTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Spider s1 = new Spider();
        s1.search("https://www.w3schools.com/html/", "html");
    }

}

共有1个答案

斜成济
2023-03-14

基于stacktrace,您从命令行运行java程序,而忘记将jsoup添加到类路径中。试跑

Java-CP类:libs/jsoup.jar com.copiedcrawler.spiderTest

其中类是编译的程序,而libs是一个带有库的文件夹。

 类似资料:
  • 我的Java应用程序包含三个具有依赖项的类文件 我已经使用maven打包了我的应用程序。 POM.xml 下面是我的清单文件内容 当我尝试使用java-jar###. jar执行打包的JAR文件时,我收到了如下错误 上面的错误是指线 打包的 JAR 包含所有必需的类文件。但我仍然面临这个错误。我的回声$CLASSPATH什么都没有。

  • 我在线程“main”java中遇到异常。错误:未解析编译。 我做错了什么? 线程“main”中出现异常: java.lang.错误:未解决的编译问题: 对于类型在

  • 问题内容: 我使用ant脚本构建了一个可执行jar,我使用的唯一外部jar是joda-time 2.0。由于我没有收到任何编译错误,因此该蚂蚁构建脚本“似乎”可以正常工作,如果我要从lib目录中删除jode-time 2.0.jar,则构建确实会按预期失败。无论如何,构建jar之后,当我尝试运行时会出现此错误: java -jar myapp.jar 线程“主”中的异常java.lang.NoCl

  • 问题内容: 我正在尝试使用Eclipse在Maven项目中作为Java应用程序运行,并且遇到以下运行时错误。错误如下所示。 线程“主”中的异常java.lang.NoClassDefFoundError:com.wang.testMaven.App.main(App.java:16)上的org / apache / http / ConnectionReuseStrategy造成原因:java.l

  • 我从GitHub克隆了我的项目,现在我无法运行我的项目。 我收到以下错误: 知道是什么导致了这个错误吗? 编辑 我检查了这个问题Intellij在我发布到这里之前突然抛出ClassNotFoundException,它没有给我任何解决方案...为什么要将我的问题标记为重复,因为那个问题不能解决我的问题?

  • 我得到了这个错误: 线程“main”Java.lang.NosuchFielderRror:calificacion.main处的num1异常(problema_3.Java:17) 在这个简单的终端Java程序上键入第一个值后: 另一方面,这一个运行良好,我使用相同的输入法在这两个: 提前道谢。