Java 类未找到异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7008877/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:15:22  来源:igfitidea点击:

Java class not found exception

javanoclassdeffounderror

提问by Luchian Grigore

I'm trying to run a Java program and somewhere along the execution, I get a

我正在尝试运行一个 Java 程序,在执行过程中的某个地方,我得到了一个

java.lang.NoClassDefFoundError: antlr/TokenStream

exception. I'm new to java programming so don't really know what this means. I've looked through some other questions about the same issues and they didn't really help me out - either I couldn't follow the answer or it didn't apply in my case. Any ideas?

例外。我是 Java 编程的新手,所以真的不知道这意味着什么。我已经查看了一些关于相同问题的其他问题,但它们并没有真正帮助我 - 要么我无法遵循答案,要么它不适用于我的情况。有任何想法吗?

回答by fmucar

search for antlr.jar and place it to your classpath

搜索 antlr.jar 并将其放置到您的类路径中

回答by Nivas

java.lang.NoClassDefFoundErroris thrown when a particular class referenced by your program is not available in the classpath. Classpath is the list of paths/directories that the runtime searches for the classes used in the class being run.

java.lang.NoClassDefFoundError当您的程序引用的特定类在类路径中不可用时抛出。类路径是运行时搜索正在运行的类中使用的类的路径/目录列表。

The error message you get means that antlr/TokenStreamis not available in your classpath.

您收到的错误消息意味着它antlr/TokenStream在您的类路径中不可用。

To include the corresponding jar (antlr.jar) to the classpath, you ca use the -cpflag while running:

要将相应的 jar ( antlr.jar) 包含到类路径中,您可以-cp在运行时使用该标志:

java -cp .;path_to_antlr.jar yourClass

Or

或者

java -cp .;path_to_antlr.jar -jar yourJar.jar

回答by Elliott

You have made reference to a java class but for some reason, that class does not exist in the execution image you are running. For example, if you instantiated a new class XYZ like: XYZ xyz = new XYZ (), but no such class existed you would get an error similar to the above. In the past, I've received this kind of error if I misspelled a reference to a class or more typically, if the class to which I was referring somehow did not get included in my jar. Check the jar or the directory in which you are doing the execution. Do you see the class to which you are making reference there? I bet it is missing.

您已经引用了一个 java 类,但由于某种原因,您正在运行的执行映像中不存在该类。例如,如果您实例化一个新类 XYZ,如:XYZ xyz = new XYZ (),但不存在此类,您将收到与上述类似的错误。过去,如果我拼错了对类的引用,或者更常见的是,如果我所指的类以某种方式没有包含在我的 jar 中,我会收到这种错误。检查 jar 或您正在执行的目录。你看到你在那里参考的班级了吗?我敢打赌它不见了。

Elliott

埃利奥特

回答by sgokhales

It is searching for the defn of the class, which it is not finding in the classpath.

它正在搜索类的定义,但在类路径中找不到。

From the JavaDoc's,

JavaDoc 的

Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

如果 Java 虚拟机或 ClassLoader 实例尝试加载类的定义(作为正常方法调用的一部分或作为使用 new 表达式创建新实例的一部分)并且找不到类的定义,则抛出。

回答by Shamim Hafiz

Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

如果 Java 虚拟机或 ClassLoader 实例尝试加载类的定义(作为正常方法调用的一部分或作为使用 new 表达式创建新实例的一部分)并且找不到类的定义,则抛出。

编译当前正在执行的类时,搜索到的类定义存在,但无法再找到该定义。

Take from here.

这里拿走。