Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 584
Arun Michael Avatar asked Jun 13 '26 12:06

Arun Michael


1 Answers

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 );
like image 79
shizhen Avatar answered Jun 15 '26 01:06

shizhen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!