Access to dll methods in java

I am trying to access dll methods in java that was written in C #. From the following code, I am trying to create a dll that is generated successfully.

using System; using Microsoft.Win32; namespace CyberoamWinHelper { public class RegistryAccess { public static String getValue(String key) { RegistryKey rk = Registry.CurrentUser; RegistryKey rk1=rk.OpenSubKey("Software\\Test", RegistryKeyPermissionCheck.ReadWriteSubTree, System.Security.AccessControl.RegistryRights.FullControl); rk1.SetValue(key, "val1"); return rk1.GetValue(key).ToString(); } public static void createSubkey(String name) { RegistryKey rk = Registry.CurrentUser; rk.CreateSubKey("Software\\Test"); } } } 

After that, I load the generated dll into my java program code, which looks like this

 public class JNI { /** * @param args the command line arguments */ public native String getValue(String key); public static void main(String[] args) { // TODO code application logic here try { System.loadLibrary("CyberoamWinHelper"); JNI j=new JNI(); System.out.println(j.getValue("abc")); } catch(UnsatisfiedLinkError e) { System.out.println("Ex" + e.getMessage()); } } } 

After running this code, it causes the following error.

 "Exjni.JNI.getValue(Ljava/lang/String;)Ljava/lang/String;" 

Well, I donโ€™t understand what this error says, but I want to solve it. And another question that I have is that the method I'm calling is a static method, will it be called this way? I want to call the static method we need

"classname.methodname"

So he can call the method?

+6
source share
4 answers

You can only call methods through the JNI if those methods are actually intended to be called this way. Your methods are not at all. What you are doing here (sorry for being so rude), absolutely no chance of success - it just doesnโ€™t work out that way.

There are several ways you can continue. One could learn about JNI and how to write libraries that actually work with it. Here is a canonical reference for this. However, using C # adds another level of complexity.

Another way would be to drop JNI altogether and use a more appropriate mechanism for accessing methods. Here you can find out about JNA ; It will be best for your purposes.

+7
source

Try jni4net . From their site. Below is a detailed explanation โ†’ How a call from Java to .NET works in jni4net

+1
source

Using JNI is wrong. It is difficult (though not impossible) to use JNI with C # libraries. There is an excellent tutorial on how to do this here . Doing a JNI search in C # on google should show more.

You should also explore something like Grasshopper ..

EDIT

http://caffeine.berlios.de/site/documentation/quickstart.html

is a cool decision.

0
source

Useful site for you: http://www.sahirshah.com/java/jni.html

Try:

 public class myJNI { /** * @param args the command line arguments */ public static native String getValue(String key); static { System.loadLibrary("CyberoamWinHelper"); } public static void main(String[] args) { // TODO code application logic here try { String myKey = "abc"; System.out.println(getValue(myKey)); } catch(UnsatisfiedLinkError e) { System.out.println("Ex" + e.getMessage()); } } } 

You need to wrap the dll in a C ++ dll, as described in the link above. Simply generate the header file using the command "javah -jni myJNI" and create a C ++ dll with the signature of the function found in this header file.

Take a look at http://www.codeproject.com/KB/cross-platform/javacsharp.aspx for a specific hello world example in C #

0
source

Source: https://habr.com/ru/post/904716/


All Articles