Home java.lang.ClassNotFoundException: org.jsoup.nodes.Document using Maven
Reply: 3

java.lang.ClassNotFoundException: org.jsoup.nodes.Document using Maven

Paagalpan Published in 2015-11-10 15:24:16Z

I'm using maven to manage my project.

This is my main file:

package com.sample;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

 * Hello world!
public class App {
    public static void main(String[] args )

        /*  String html = "<html><head><title>First parse</title></head>"
                      + "<body><p>Parsed HTML into a doc.</p></body></html>";
                    Document doc = Jsoup.parse(html);*/
            Document doc = Jsoup.connect("http://www.javatpoint.com").get();  
            String title = doc.title();  
            System.out.println("title is: " + title);  
        catch (Exception e)

This is my pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">





When I run the following commands:

mvn clean package
java -jar target/sample-pc-1.0-SNAPSHOT.jar 

I get the following errors:

Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/nodes/Document at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) at java.lang.Class.privateGetMethodRecursive(Class.java:3048) at java.lang.Class.getMethod0(Class.java:3018) at java.lang.Class.getMethod(Class.java:1784) at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526) Caused by: java.lang.ClassNotFoundException: org.jsoup.nodes.Document at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.

However, if I directly run the app from eclipse, it runs without any errors. I am not sure why I cant run it from the command line.


Avihoo Mamka
Avihoo Mamka Reply to 2015-11-10 15:59:42Z

Your JAR does not include your dependencies after it has been packaged.

Add this code to your pom.xml under the plugins section:


And add the file assembly.xml to your resources directory:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <!-- TODO: a jarjar format would be better -->

And then you can run mvn clean clean install or mvn package

bmargulies Reply to 2015-11-10 15:28:23Z

Specifying a dependency to Maven makes it available for compiling -- not for running your code afterwards. Eclipse reads your POM and also sees the dependency.

Read up on the appassembler-maven-plugin or about making a MANIFEST.MF with a Class-Path to solve your problem.

N K Reply to 2015-11-10 22:15:33Z

As an aside, I'm using IntelliJ IDEA with Maven support, and my setup is very similar to yours except for the scope: compile element in the dependency declaration. Looks like the IDE takes care of these specific requirements.

Unless you are specifically looking to understand Maven, you could consider using such an IDE to handle the minutiae for you.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.332455 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO