繁体   English   中英

不确定NoClassDefFoundError背后的原因

[英]Unsure about reason behind NoClassDefFoundError

我已经构建了一个DLL,用于包装Java代码,但是在运行Java程序时遇到了一些麻烦。 我编写了一个简单的测试DLL和Java程序,并产生了相同的错误,尽管在线上有很多有关NoClassDefFoundError的资源,但我似乎无法用任何故障排除方法来解决我的问题。

这是我的D:\\Test1.Java文件

public class Test1 { 

    static {
        //System.loadLibrary("HeyLand");
        System.load("D://HeyLand.dll");
    }

    public native void displayHeyLand();

    public static void main (String[] args) {
        Test1 t = new Test1();
        t.displayHeyLand();
    }
}

编译之后,尝试运行D:\\Test1.class导致以下结果:

D:\>java Test1.class
Exception in thread "main" java.lang.NoClassDefFoundError: Test1.class
Caused by: java.lang.ClassNotFoundException: Test1.class
        at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:660)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:626)
Could not find the main class: Test1.class.  Program will exit.

为什么我感到难过:
1.我将类路径设置为D:\\ ,所以我相信我的类定义将在类路径中,并且我看不到编译时类路径和运行时类路径可能有什么不同。
2.我看不到这与静态初始化有什么关系,我相信异常看起来会有所不同。

也许我只是错过了一个非常简单的东西,我是Java的新手。

任何帮助是极大的赞赏!

classpath环境变量的优先级高于java run命令中的环境变量。 您需要指定类的位置(以及删除.class文件扩展名)

java -cp . Test1
Java normal syntax for executing class file is 
Java [<options>....} <class-name> [<arguments>....]


For example 

java com.package.name.Test1

here how compiler works

1. Compiler search for complete class name
2. Load that class
3. check for main method - in the same class
4. Call main method with passed arguments in command line string.



Now following are the possibilities why your class may not found main method.
1 - forgot to include package name
    I am new developer in java but I found when I run application using eclips or intellJ editor it gives different path and package name and execute code as I noticed it on command line edior. So make sure you are including package name 

    For example:
    java com.package.name.Test1 instead of 
    java Test1 

2. File name or pathname rather then class name
   As I noticed output file is in different location. That why class file path was different.
   java Test1.class
   java com/package/name/Test1.class

3. Typo


also I noticed you are using 
  static {
        //System.loadLibrary("HeyLand");
        System.load("D://HeyLand.dll");
    }


 Is this function ? or constructor? If it is function then where is name of the function? You cant write code without any reference in classs

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM