2017-07-05 101 views
0

我想在Windows中編寫事件,並且我的應用程序僅支持groovy。Groovy腳本無法找到JNA類

我寫的短代碼來測試,但我得到了以下異常總是:

Java.lang.NoClassDefFoundError: Could not initialize class com.sun.jna.platform.win32.Advapi32 
    at ProcessStateHandler.registerEventSource(test.groovy:32) 
    at ProcessStateHandler.main(test.groovy:23) 

測試代碼是這樣的:

import com.sun.jna.platform.win32.Advapi32 
import com.sun.jna.platform.win32.Advapi32Util 
import com.sun.jna.platform.win32.Kernel32 
import com.sun.jna.platform.win32.Win32Exception 
import com.sun.jna.platform.win32.WinNT 
import com.sun.jna.platform.win32.WinNT.HANDLE 
import com.sun.jna.platform.win32.WinReg 

import com.sun.jna.Native 
import com.sun.jna.Library 
import com.sun.jna.Platform 
import com.sun.jna.Pointer 



public class ProcessStateHandler { 
    def APPLICATION_EVENT_LOG_REGISTRY = "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application" 
    public static void main(String[] args) { 
     println "First groovy test" 
     CLibrary.INSTANCE.printf("Hello, World\n") 

     ProcessStateHandler processStateHandler = new ProcessStateHandler() 
     processStateHandler.registerEventSource("", "SourceForTest") 
    } 

    private void registerEventSource(String server, String source) { 

     source = source.trim() 

     assert Advapi32 
//  assert Advapi32.INSTANCE   
     HANDLE eventHandle = Advapi32.INSTANCE.RegisterEventSource(server, source) 
    } 
} 


public interface CLibrary extends Library { 
    CLibrary INSTANCE = (CLibrary) Native.loadLibrary((Platform.isWindows() ? 'msvcrt' : 'c'), CLibrary.class) 

    void printf(String format, Object... args) 
} 

在我的猜測,常規不支持變量接口。 Advapi32接口是這樣聲明的。

public interface Advapi32 extends StdCallLibrary { 
    Advapi32 INSTANCE = (Advapi32) Native.loadLibrary("Advapi32", 
      Advapi32.class, W32APIOptions.UNICODE_OPTIONS); 
    /* blah 
     blah 
     blah */ 
} 

如何在Advapi32接口中訪問INSTANCE或調用方法?

回答

0

也許,我的測試是錯誤的。

當我在Eclipse中測試代碼時,沒有問題。 GroovyConsole可能有類加載器問題。