簡體   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