gpt4 book ai didi

Java 类路径问题

转载 作者:行者123 更新时间:2023-12-02 00:28:06 26 4
gpt4 key购买 nike

我已经编译完毕,现在正在尝试运行 this在 ie 目录中使用以下命令从 OSX 和 Fendora 的终端运行程序:

java ie.moguntia.webcrawler.Psucker http://www.wikipedia.org 测试

但是我收到以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: ie/moguntia/webcrawler/Psucker
Caused by: java.lang.ClassNotFoundException: ie.moguntia.webcrawler.Psucker
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

我之前没有运行过从命令行使用包的应用程序,因此我无法找出错误。

我尝试使用 export CLASSPATH=. 设置类路径,以便将当前目录添加到类路径中。

目录结构如下:

ie/moguntia/webcrawler/

最佳答案

根据您编译的类所在的位置,您需要在 java 命令中包含此位置,例如如果它们位于 classes 目录中:

java -cp classes ie.moguntia.webcrawler.PSucker http://www.wikipedia.org test

或者如果它们位于当前目录中:

java -cp . ie.moguntia.webcrawler.PSucker http://www.wikipedia.org test

或者只是

java ie.moguntia.webcrawler.PSucker http://www.wikipedia.org test

这是我如何运行它的(解压缩,cd到目录,编译,查找类,运行):

(13:55:52) ~/Desktop/temp → ll
total 32
-rw-r--r--@ 1 Nils.Winkler staff 14K 12 Mär 13:41 multiweb.zip

(13:56:01) ~/Desktop/temp → unzip multiweb.zip -d multiweb
Archive: multiweb.zip
creating: multiweb/CVS/
creating: multiweb/ie/
creating: multiweb/ie/CVS/
creating: multiweb/ie/moguntia/
creating: multiweb/ie/moguntia/CVS/
creating: multiweb/ie/moguntia/threads/
creating: multiweb/ie/moguntia/threads/CVS/
inflating: multiweb/ie/moguntia/threads/ControllableThread.java
inflating: multiweb/ie/moguntia/threads/MessageReceiver.java
inflating: multiweb/ie/moguntia/threads/ObjectQueue.java
inflating: multiweb/ie/moguntia/threads/Queue.java
inflating: multiweb/ie/moguntia/threads/ThreadController.java
creating: multiweb/ie/moguntia/webcrawler/
creating: multiweb/ie/moguntia/webcrawler/CVS/
inflating: multiweb/ie/moguntia/webcrawler/PSucker.java
inflating: multiweb/ie/moguntia/webcrawler/PSuckerThread.java
inflating: multiweb/ie/moguntia/webcrawler/SaveURL.java
inflating: multiweb/ie/moguntia/webcrawler/URLQueue.java
inflating: multiweb/ie/moguntia/webcrawler/WSDLCrawler.java
inflating: multiweb/ie/moguntia/webcrawler/WSDLCrawlerThread.java

(13:56:08) ~/Desktop/temp → cd multiweb

(13:56:57) ~/Desktop/temp/multiweb → javac ie/moguntia/webcrawler/*.java
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

(13:57:11) ~/Desktop/temp/multiweb → find . -name *.class
./ie/moguntia/threads/ControllableThread.class
./ie/moguntia/threads/MessageReceiver.class
./ie/moguntia/threads/Queue.class
./ie/moguntia/threads/ThreadController.class
./ie/moguntia/webcrawler/PSucker.class
./ie/moguntia/webcrawler/PSuckerThread.class
./ie/moguntia/webcrawler/SaveURL.class
./ie/moguntia/webcrawler/URLQueue.class
./ie/moguntia/webcrawler/WSDLCrawler.class
./ie/moguntia/webcrawler/WSDLCrawlerThread.class

(13:57:18) ~/Desktop/temp/multiweb → java ie.moguntia.webcrawler.PSucker http://www.wikipedia.org test
[0] http://www.wikipedia.org

关于Java 类路径问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9666987/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com