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

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

Paagalpan
1#
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 )
    {
        try
        {

        /*  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)
        {
            System.out.println(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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sample</groupId>
    <artifactId>sample-pc</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>sample-pc</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.8.3</version>
                    <scope>compile</scope>

        </dependency>

    </dependencies>

        <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.sample.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

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.

Thanks

Avihoo Mamka
2#
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:

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <descriptor>assembly.xml</descriptor>
        </configuration>
    </plugin>

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

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          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 -->
    <id>jar-with-dependencies</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>true</unpack>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>

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

bmargulies
3#
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
4#
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