是什么导致了 NoSuchMethodError 和 NoClassDefFoundError?
当我运行 test.class 时,出现以下错误:
Exception in thread "main" java.lang.NoSuchMethodError: ml.Temp.<init>(Ljava/lang/String;II)V
at test.main(test.java:11)
这是 test.java 的代码
import java.io.*;
import ml.*;
class test
{
public static void main(String[] args) throws FileNotFoundException, IOException
{
String filename = "input";
Temp id = new Temp(filename, 6, 100);
id.someFunction();
}
}
本质上我有一个包含 Temp.class 的 jar 文件(Temp 是我编写的库文件,位于 ml 包下)。 Temp 有一个构造函数,它接受这三个参数和一个公共 someFunction。
不确定这是否有帮助,但我在编译过程中包含了 jar 文件的类路径。当我在 test.class 运行期间包含 jar 文件的类路径时,我得到以下
Exception in thread "main" java.lang.NoClassDefFoundError: test
Caused by: java.lang.ClassNotFoundException: test
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: test. Program will exit.
编辑:
如果它有帮助,我编译如下(ml.jar 和 test.java 位于同一目录中)
javac -cp ml.jar test.java
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该运行命令
java -cp; test
来自test.class
所在的路径。编辑
NoSuchMethodError
表示在test
类的第 11 行中,您尝试使用ml.ID3
的构造函数> (不是Temp
!),其签名(String, int, int
)不存在。这并不意味着它不在类路径中,因为这会导致NoClassDefFoundError
。另一方面,
NoClassDefFoundError: test
意味着test
不再位于您的 clsspath 中。将 jar 添加到类路径中,如果您所做的这一切确实是为了获得NoClassDefFoundError: test
,不能导致test
从您的类路径...因此您更有可能执行了其他一些无意的操作。编辑2
也许该构造函数存在于您的开发环境中使用的
ID3
中,但它显然不存在于您的运行时环境中。如果它是第 3 方 jat,则可能会出现该 jar 的两个版本,一个在开发中使用,另一个在运行时使用。或者甚至可能两者都出现在您的运行时环境中,但缺少构造函数工具优先级。you should run the command
java -cp <path-to-ml.jar> test
from the path in whichtest.class
resides.EDIT
The
NoSuchMethodError
indicates that in line 11 of classtest
you're trying to use a constructor ofml.ID3
(notTemp
!) with a signature (String, int, int
) that doesn't exist. It doesn't mean it's not in the classpath because that would result inNoClassDefFoundError
.On the other hand, the
NoClassDefFoundError: test
means thattest
is not in your clsspath anymore. Adding the jar to the classpath, if this all you've really done to get theNoClassDefFoundError: test
, cannot causetest
to disappear from your classpath... So it's more likely that you've done some other inadvertent action.EDIT 2
Maybe that constructor exists in the
ID3
used in your development environment but it evidently doesn't exist in your runtime environment. If it's a 3rd party jat, might happen you have two versions of that jar, one used in dev and the other in runtime. Or maybe even both appear in your runtime environment but the one that lacks that constructor tool precedence.如果没有源代码,很难准确地告诉您发生了什么,但看起来您的库和使用它的项目不同步。您是否更改了库/项目中任何方法的签名?我建议重新编译所有内容,然后检查类
test
是否使用您重新编译的库的最新版本以及其是否声明为public
。有关如何阅读
NoSuchMethodError
等的更多有用信息,可以在此处的一篇精彩文章中找到:http://www.cubrid.org/blog/dev-platform/understanding-jvm-internals/Its difficult to tell you exactly what happened without the source code but it looks like your library and the project which is using it, are out of synch. Have you changed the signatures of any methods in your library/project? I would recommend to recompile everything and then check if the class
test
is using the latest version of the library you have recompiled as well as if its declaredpublic
.More useful information on how to read
NoSuchMethodError
etc can be found in a great article here: http://www.cubrid.org/blog/dev-platform/understanding-jvm-internals/我将假装成你并完成整个过程:
1 - 图书馆。我位于一个名为
workspace
的目录中。在其中,我创建了一个名为ml
的目录。在
ml
目录中,我创建了一个名为Temp.java
的新文件,作为示例,其中包含以下内容:我现在要编译库类并创建一个库 jar。
首先,我通过
在
ml
目录中运行来进行编译。然后,我通过将一个目录转到workspace
目录并运行:2 - The Program 来 jar 库。我现在在
workspace
目录中创建Test.java
文件。该文件包含以下内容:3 - 结果。我现在继续编译并运行我的测试程序。在我运行的
workspace
目录中:我终于可以通过在
workspace
目录中运行以下命令来运行该程序:这将显示输出:
我希望这会有所帮助。
I am going to pretend to be you and go through the whole process:
1 - The Library. I am in a directory called, say,
workspace
. Inside it, I have created a directory calledml
.Inside the
ml
directory I have created a new file calledTemp.java
which, for the sake of example, contains the following:I am now going to compile the library class and create a library jar.
First I compile by running
inside the
ml
directory. Then I jar the library by going one directory up to theworkspace
directory and running:2 - The Program. I now create the
Test.java
file inside theworkspace
directory. The file contains the following:3 - The Result. I now proceed to compile and run my test program. Inside the
workspace
directory I run:I can finally run the program by running the following command inside the
workspace
directory:Which would show the output:
I hope this helps.