简体   繁体   中英

java - error while running compiled program with external jar

I have a simple jsoup test app in a single folder, in which there are 2 classes - LyricsGetter.java and Main.java - and a .jar file with jsoup library. When I compile the files with command javac -cp jsoup-1.8.2.jar LyricsGetter.java Main.java everything compiles ok, but when I try to run with java Main , I get:

Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/Jsoup
    at LyricsGetter.getLyrics(LyricsGetter.java:16)
    at Main.main(Main.java:14)
Caused by: java.lang.ClassNotFoundException: org.jsoup.Jsoup
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 2 more

On the other hand if I run with java -cp jsoup-1.8.2.jar Main , then I get Error: Could not find or load main class Main . So, what is the correct way to run this program?

You need to tell Java to look for classes in jsoup-1.8.2.jar , and the current folder ( . ).

On Windows, use:

java -cp jsoup-1.8.2.jar;. Main

or on Linux, OSX, or other Unix-like systems use:

java -cp jsoup-1.8.2.jar:. Main

(The difference is that the paths are separated by ; on Windows or : on *nix)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM