java“ClassNotFoundException”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10664482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java "ClassNotFoundException" error
提问by Steve Quezadas
I am new to java programming and I am getting the much-maligned error "ClassNotFoundException" error.
我是 Java 编程的新手,并且收到了备受诟病的错误“ClassNotFoundException”错误。
The strange thing is is that it compiles fine:
奇怪的是它编译得很好:
java -cp /usr/share/java/scribe-1.3.0.jar FacebookProg
But when I try to run it, I get the following error:
但是当我尝试运行它时,出现以下错误:
steve@steve-ThinkPad-T61:~/facebook$ java FacebookProg
Exception in thread "main" java.lang.NoClassDefFoundError:
org/scribe/builder/ServiceBuilder
at FacebookProg.main(FacebookProg.java:8)
Caused by: java.lang.ClassNotFoundException: org.scribe.builder.ServiceBuilder
at java.net.URLClassLoader.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)
... 1 more
I checked online and it seems that java can't find the library at runtime that it was able to find at compile time. So tried the following variations:
我在网上查了一下,似乎java在运行时找不到它可以在编译时找到的库。所以尝试了以下变体:
java -cp /usr/share/java/scribe-1.3.0.jar FacebookProg
java -cp /usr/share/java/ FacebookProg
export CLASSPATH="/usr/share/java"; java FacebookProf
export CLASSPATH="/usr/share/java/usr/share/java/scribe-1.3.0.jar"; java FacebookProg
I checked several places on StackOverflow and google and still can't figure out why. I'm new to java, so there's probably a simple solution, but I can't find it. I am using Sun Java 1.6 64-bit on Ubuntu 11.04. The scribe-1.3.0.jar file is in "/usr/share/java" which, I believe, is the canonical place to put java packages.
我在 StackOverflow 和 google 上检查了几个地方,但仍然无法弄清楚原因。我是 Java 新手,所以可能有一个简单的解决方案,但我找不到。我在 Ubuntu 11.04 上使用 Sun Java 1.6 64 位。scribe-1.3.0.jar 文件位于“/usr/share/java”中,我相信这是放置 java 包的规范位置。
The barebones code is here (in case it matters):
准系统代码在这里(以防万一):
import org.scribe.builder.*;
import org.scribe.builder.api.*;
import org.scribe.oauth.*;
public class FacebookProg {
public static void main (String args[]) {
OAuthService service = new ServiceBuilder()
.provider(FacebookApi.class)
.apiKey("blah_blah")
.apiSecret("blah_blah")
.build();
}
}
回答by EnKrypt
The classpath has to point to BOTH the directory of the external library you are using AND the class you are trying to run itself. Try this:
类路径必须指向您正在使用的外部库的目录和您试图自行运行的类。试试这个:
Windows:
视窗:
java -cp .;/usr/share/java/scribe-1.3.0.jar FacebookProg
Linux:
Linux:
java -cp .:/usr/share/java/scribe-1.3.0.jar FacebookProg
By the way , to compile it you should have run this:
顺便说一句,要编译它,您应该运行以下命令:
javac -cp /usr/share/java/scribe-1.3.0.jar FacebookProg
回答by Attila
This
这
java -cp /usr/share/java/scribe-1.3.0.jar FacebookProg
means you are running the FacebookProg
class, not compiling it.
意味着您正在运行FacebookProg
该类,而不是编译它。
If you leave the -cp ...
out, you are leaving the vital classpath out, so the JVM cannot find the classes FacebookProg
requires.
如果你忽略-cp ...
了,你就忽略了重要的类路径,所以 JVM 找不到FacebookProg
需要的类。
To compile, you need
要编译,你需要
javac -cp /usr/share/java/scribe-1.3.0.jar FacebookProg.java
(note the javac
, instead of java
to invoke the compiler)
(注意javac
, 而不是java
调用编译器)
To run, you already know how to.
要跑步,你已经知道怎么做。
Also, you have errors in the follwoing lines:
此外,您在以下几行中存在错误:
export CLASSPATH="/usr/share/java"; java FacebookProf
export CLASSPATH="/usr/share/java/usr/share/java/scribe-1.3.0.jar"; java FacebookProg
The first misspells FacebookProg
and does not have the required jar on the classpath, the second has the wrong path to the jar. Try
第一个拼写错误FacebookProg
并且在类路径上没有所需的 jar,第二个有错误的 jar 路径。尝试
export CLASSPATH="/usr/share/java/scribe-1.3.0.jar"; java FacebookProg
Also, make sure the jar is indeed located at /usr/share/java/scribe-1.3.0.jar
另外,请确保罐子确实位于 /usr/share/java/scribe-1.3.0.jar
回答by Yanjiong Wang
suppose that, the directory of your program is
假设,你的程序目录是
$HOME
$HOME
$HOME/lib/*.jar
$HOME/lib/*.jar
you can write a script like:
您可以编写如下脚本:
for file in "$HOME/lib/*.jar"
do
if [ -f $file ]
then
CLASSPATH=$CLASSPATH:$file
else
echo ignore $file
fi
done
java -cp $CLASSPATH FacebookProg
回答by Ashwinee K Jha
java -cp /usr/share/java/scribe-1.3.0.jar FacebookProg
This should work fine if you compiled the FacebookProg.class in same directory. You can try java -cp /usr/share/java/scribe-1.3.0.jar:/locationOfFacebookProg.class directory/ FacebookProg
如果您在同一目录中编译了 FacebookProg.class,这应该可以正常工作。你可以试试java -cp /usr/share/java/scribe-1.3.0.jar:/locationOfFacebookProg.class directory/ FacebookProg