简体   繁体   English

java-使用外部jar运行编译的程序时出错

[英]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. 我在单个文件夹中有一个简单的jsoup测试应用程序,其中有2个类-LyricsGetter.java和Main.java-以及带有jsoup库的.jar文件。 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: 当我使用命令javac -cp jsoup-1.8.2.jar LyricsGetter.java Main.java编译文件时,一切都可以编译,但是当我尝试使用java Main运行时,得到:

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 . 另一方面,如果我使用java -cp jsoup-1.8.2.jar Main ,则会出现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 ( . ). 您需要告诉Java在jsoup-1.8.2.jar 和当前文件夹. )中查找类。

On Windows, use: 在Windows上,使用:

java -cp jsoup-1.8.2.jar;. Main

or on Linux, OSX, or other Unix-like systems use: 或在Linux,OSX或其他类似Unix的系统上使用:

java -cp jsoup-1.8.2.jar:. Main

(The difference is that the paths are separated by ; on Windows or : on *nix) (不同之处在于,路径在Windows上用;或在* nix上用:分隔)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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