Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing dll methods in java

I am trying to accessing dll methods in java which has been written in c#. From the following code i am trying to build dll which 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 this i am loading the generated dll in my java program code of which is as follows

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 is giving me the following error.

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

Well i am not understanding what this error is saying but i want to solve it. And one more question i am having is since the method i am calling is a static method will it be called in this way? i mean to call static method we need

"classname.methodname"

so here will it be able to call the method?

like image 882
Ankur Trapasiya Avatar asked Dec 30 '11 01:12

Ankur Trapasiya


2 Answers

You can only call methods via JNI if those methods were in fact designed to be called this way. Your methods absolutely are not. What you're doing here has (sorry to be so blunt) absolutely no chance of ever succeeding -- it simply doesn't work this way.

There are several ways you might proceed. One would be to learn about JNI and how to write libraries that actually work with it. Here is the canonical reference for this. Doing this with C# adds yet another layer of complexity, though.

Another way would be to give up on JNI altogether and use a more appropriate mechanism to access the methods. You can learn about JNA here; it would be entirely better suited to your goals.

like image 169
Ernest Friedman-Hill Avatar answered Sep 19 '22 00:09

Ernest Friedman-Hill


Try jni4net. From their web site. Some detailed explanation is here -> How calling from Java to .NET works in jni4net

like image 21
Jayan Avatar answered Sep 20 '22 00:09

Jayan