Frage

I am currently having a problem I believe deals with some classpath problems but I haven't found any way to fix it. I currently have the following java code:

import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class JsoupTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {

        String url = "http://en.wikipedia.org/wiki/Main_Page";

        Document doc = Jsoup.connect(url).get();

        String result = doc.text();

        System.out.println(result);
    }

}

I have the jsoup-1.7.3.jar file in the same directory as the java file(to keep things simple).

I ran the command "javac -cp .:jsoup-1.7.3.jar JsoupTest.java" which compiles and works fine. However when I go and run "java JsoupTest" I get the following error

java JsoupTest

Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/Jsoup
    at JsoupTest.main(JsoupTest.java:31)
Caused by: java.lang.ClassNotFoundException: org.jsoup.Jsoup
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 1 more

What is the possible error and how exactly would I go about fixing it? I believe it's a classpath issue but the classpath should already be set up when I compiled it? Any help would be appreciated. Thank you.

War es hilfreich?

Lösung

Just like the compilation command you need to include the jar in the classpath

java -cp .:jsoup-1.7.3.jar JsoupTest

For windows use .; instead of .:

java -cp .;jsoup-1.7.3.jar JsoupTest

Andere Tipps

Try:

java -cp .:jsoup-1.7.3.jar JsoupTest
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top