6

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?

ATR
  • 2,160
  • 4
  • 22
  • 43

4 Answers4

7

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.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
1

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

Jayan
  • 18,003
  • 15
  • 89
  • 143
0

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.

Ahmed Masud
  • 21,655
  • 3
  • 33
  • 58
  • Well first link is not what i am trying to do. It's about visual c++ and java. That is done through JNI but i want to do it between c# and java with or without JNI. And in the second link is also there is no answer to my question. – ATR Dec 30 '11 at 01:22
  • 1
    JNI has NO direct interface with C# .. You have to create a C++ stub between C# and Java. – Ahmed Masud Dec 30 '11 at 01:25
  • OOooh i found a cool solution for you. jni4net is nice as specified by Jayan but... – Ahmed Masud Dec 30 '11 at 01:27
0

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#

Motomotes
  • 4,111
  • 1
  • 25
  • 24