是什么导致了 NoSuchMethodError 和 NoClassDefFoundError?

发布于 2025-01-03 21:46:25 字数 1424 浏览 2 评论 0 原文

当我运行 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

When I run test.class I get the following error:

Exception in thread "main" java.lang.NoSuchMethodError: ml.Temp.<init>(Ljava/lang/String;II)V
    at test.main(test.java:11)

And here is the code for 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();          
         }
}

Essentially I have a jar file containing Temp.class (Temp is a library file that I wrote and which is under the ml package). Temp has a constructor which takes these three arguments and a public someFunction.

Not sure if this helps, but I included the classpath of the jar file during the compilation. When I include the classpath of the jar file during running of test.class I get the following

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.

EDIT:

If it helps I compiled as follows (ml.jar and test.java are in the same directory)

javac -cp ml.jar test.java

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

糖果控 2025-01-10 21:46:25

您应该运行命令 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 which test.class resides.

EDIT

The NoSuchMethodError indicates that in line 11 of class test you're trying to use a constructor of ml.ID3 (not Temp!) with a signature (String, int, int) that doesn't exist. It doesn't mean it's not in the classpath because that would result in NoClassDefFoundError.

On the other hand, the NoClassDefFoundError: test means that test is not in your clsspath anymore. Adding the jar to the classpath, if this all you've really done to get the NoClassDefFoundError: test, cannot cause test 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.

夜光 2025-01-10 21:46:25

如果没有源代码,很难准确地告诉您发生了什么,但看起来您的库和使用它的项目不同步。您是否更改了库/项目中任何方法的签名?我建议重新编译所有内容,然后检查类 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 declared public.

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/

凑诗 2025-01-10 21:46:25

我将假装成你并完成整个过程:

1 - 图书馆。我位于一个名为 workspace 的目录中。在其中,我创建了一个名为 ml 的目录。
ml 目录中,我创建了一个名为 Temp.java 的新文件,作为示例,其中包含以下内容:

package ml;

public class Temp
{
    public Temp(String filename, int arg1, int arg2)
    {
        //do something
    }

    public void someFunction()
    {
        //do something else
        System.out.println("look left!");
    }
}

我现在要编译库类并创建一个库 jar。
首先,我通过

javac ./Temp.java

ml 目录中运行来进行编译。然后,我通过将一个目录转到 workspace 目录并运行:

jar cf ml.jar ml/

2 - The Program 来 jar 库。我现在在 workspace 目录中创建 Test.java 文件。该文件包含以下内容:

import java.io.*;
import ml.*;

public class Test
{
    public static void main(String[] args) throws FileNotFoundException, IOException
    {
        String filename = "input";

        Temp id = new Temp(filename, 6, 100);
        id.someFunction();
    }
}

3 - 结果。我现在继续编译并运行我的测试程序。在我运行的 workspace 目录中:

javac -cp ./:ml.jar ./Test.java

我终于可以通过在 workspace 目录中运行以下命令来运行该程序:

java -cp ./:ml.jar Test

这将显示输出:

look left!

我希望这会有所帮助。

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 called ml.
Inside the ml directory I have created a new file called Temp.java which, for the sake of example, contains the following:

package ml;

public class Temp
{
    public Temp(String filename, int arg1, int arg2)
    {
        //do something
    }

    public void someFunction()
    {
        //do something else
        System.out.println("look left!");
    }
}

I am now going to compile the library class and create a library jar.
First I compile by running

javac ./Temp.java

inside the ml directory. Then I jar the library by going one directory up to the workspace directory and running:

jar cf ml.jar ml/

2 - The Program. I now create the Test.java file inside the workspace directory. The file contains the following:

import java.io.*;
import ml.*;

public class Test
{
    public static void main(String[] args) throws FileNotFoundException, IOException
    {
        String filename = "input";

        Temp id = new Temp(filename, 6, 100);
        id.someFunction();
    }
}

3 - The Result. I now proceed to compile and run my test program. Inside the workspace directory I run:

javac -cp ./:ml.jar ./Test.java

I can finally run the program by running the following command inside the workspace directory:

java -cp ./:ml.jar Test

Which would show the output:

look left!

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文