javadlljava-native-interfacedllimportadvapi32

How to call dll files in java


I need to call a DLL file advapi32.dll in java so that i can use its CryptEncrypt function for Encryption. Is it possible to access the functions of the dll files through JNI or is there any other better ways

advapi32.dll is found in system32 folder of windows

I tried using

System.loadLibrary("advapi32");

and

Runtime.getRuntime().loadLibrary("C:/Windows/System32/crypt32.dll");

Runtime.getRuntime().loadLibrary() giving the following errors

Exception in thread "main" java.lang.UnsatisfiedLinkError: no C:/Windows/System32/crypt32.dll in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.Runtime.loadLibrary(Unknown Source)
at mail.encrypt.main(encrypt.java:17)

Solution

  • Specify the lib path using below command

    java -Djava.library.path=C:/Windows/System32/
    

    Or use below hacking way inside code

    System.setProperty( "java.library.path", "C:/Windows/System32/" );
    Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths"); 
    fieldSysPath.setAccessible( true ); 
    fieldSysPath.set( null, null );