不确定 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. 我已将我的 class 路径设置为 D:\
,所以我相信我的 class 定义会在 class 路径中,但我看不到我的编译方式- time 和 运行-time classpaths 可能有任何不同。
2. 我看不出这与静态初始化有什么关系,我相信异常看起来会有所不同。
也许我只是遗漏了一些非常简单的东西,我是 Java 的新手。
非常感谢任何帮助!
classpath 环境变量优先于 java
运行 命令中的环境变量。您需要指定 class 位置(以及删除 .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
我构建了一个 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. 我已将我的 class 路径设置为 D:\
,所以我相信我的 class 定义会在 class 路径中,但我看不到我的编译方式- time 和 运行-time classpaths 可能有任何不同。
2. 我看不出这与静态初始化有什么关系,我相信异常看起来会有所不同。
也许我只是遗漏了一些非常简单的东西,我是 Java 的新手。
非常感谢任何帮助!
classpath 环境变量优先于 java
运行 命令中的环境变量。您需要指定 class 位置(以及删除 .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