title: Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/Jsoup
body: <p>I copied a simple web crawler from the internet and then started to run the application in a test class. Every time i try to run the application I get &quot;Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: org/jsoup/Jsoup&quot; error. I first imported the jsoup jar as a externaljar in a Libary, because I needed it for the http stuff.</p>
  <p><strong>Error messages:</strong></p>
  <pre><code>Exception in thread &quot;main&quot; 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
  </code></pre>
  <p><strong>Spider Class</strong></p>
  <pre><code>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&lt;String&gt; pagesVisited = new HashSet&lt;String&gt;();
    private List&lt;String&gt; pagesToVisit = new LinkedList&lt;String&gt;();
  
    public void search(String url, String searchWord)
    {
        while(this.pagesVisited.size() &lt; 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(&quot;**Success** Word %s found at %s&quot;, searchWord, currentUrl));
                break;
            }
            this.pagesToVisit.addAll(leg.getLinks());
        }
        System.out.println(&quot;\n**Done** Visited &quot; + this.pagesVisited.size() + &quot; web page(s)&quot;);
    }
  
  
    /**
     * 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;
    }
  }
  </code></pre>
  <p><strong>SpiderLeg class</strong></p>
  <pre><code>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 =
              &quot;Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1&quot;;
      private List&lt;String&gt; links = new LinkedList&lt;String&gt;();
      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(&quot;\n**Visiting** Received web page at &quot; + url);
              }
              if(!connection.response().contentType().contains(&quot;text/html&quot;))
              {
                  System.out.println(&quot;**Failure** Retrieved something other than HTML&quot;);
                  return false;
              }
              Elements linksOnPage = htmlDocument.select(&quot;a[href]&quot;);
              System.out.println(&quot;Found (&quot; + linksOnPage.size() + &quot;) links&quot;);
              for(Element link : linksOnPage)
              {
                  this.links.add(link.absUrl(&quot;href&quot;));
              }
              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(&quot;ERROR! Call crawl() before performing analysis on the document&quot;);
              return false;
          }
          System.out.println(&quot;Searching for the word &quot; + searchWord + &quot;...&quot;);
          String bodyText = this.htmlDocument.body().text();
          return bodyText.toLowerCase().contains(searchWord.toLowerCase());
      }
  
  
      public List&lt;String&gt; getLinks()
      {
          return this.links;
      }
  
  }
  </code></pre>
  <p><strong>SpiderTest class</strong></p>
  <p>package com.copiedcrawler;</p>
  <pre><code>public class SpiderTest {
  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
          Spider s1 = new Spider();
          s1.search(&quot;https://www.w3schools.com/html/&quot;, &quot;html&quot;);
      }
  
  }
  </code></pre>