我已经构建了一个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的新手。
非常感谢任何帮助!
答案 0 :(得分:1)
classpath环境变量优先于java
run命令中的变量。您需要指定类位置(以及删除.class
文件扩展名)
java -cp . Test1
答案 1 :(得分:0)
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