Ankur Trapasiya
Ankur Trapasiya

Reputation: 2200

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?

Upvotes: 6

Views: 10400

Answers (4)

Motomotes
Motomotes

Reputation: 4237

Helpful 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 above link. Just generate a header file with the "javah -jni myJNI" command and build a c++ dll with the function signature found in that header file.

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

Upvotes: 0

Ahmed Masud
Ahmed Masud

Reputation: 22374

Your use of JNI is incorrect. It's difficult (although not impossible) to use JNI with C# libraries. There is an excellent tutorial on how to go about it here. Doing a C# JNI search on google shall reveal more.

You should also investigate something like Grasshopper..

EDIT

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

is a cool solution.

Upvotes: 0

Jayan
Jayan

Reputation: 18459

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

Upvotes: 1

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81674

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.

Upvotes: 7

Related Questions