How to pass a JNI C# class into Java or handle this situation?

前端 未结 6 2614
[愿得一人]
[愿得一人] 2021-02-20 01:42

I\'m trying to call a Java method from C#, it\'s called like this from java:

EgamePay.pay(thisActivity, payAlias, new EgamePayListener() {
            @Override
         


        
6条回答
  •  隐瞒了意图╮
    2021-02-20 02:20

    Here is JNI Field Descriptor

    JavaLanguage     Type
    --------------------------------
    Z                 boolean
    B                 byte
    C                 char
    S                 short
    I                 int
    J                 long
    F                 float
    D                 double
    Ljava/lang/String; string
    [Ljava/lang/Object; object[]
    

    Method descriptors make use of the fields descriptors and describe the structure of a Java method. There are no spaces between the field descriptors in a method descriptor.

    Apart from the void return type which is denoted by V, all other return types use the field descriptor. The table below describes the Java method declaration and the corresponding JNI descriptor. The JNI method descriptor is used when calling a Java method from C# via JNI.

     Java Method Declaration    JNI Method Descriptor
    ----------------------------------------------------
     String foo();          "()Ljava/lang/String;"
     Void bar(int I, bool b);   (IZ)V
    

    The first thing that needs to be done is to create a dictionary object that will contain all of the parameters to pass to the Java Virtual Machine. In the example below, I am doing the minimum of setting the class path that will tell the JVM where to look for the classes and packages.

    private Dictionary jvmParameters = new Dictionary();
    jvmParameters.Add("-Djava.class.path", Location of the java class);
    

    Once the JVM parameters have been assigned to the dictionary object, an instance of JavaNativeInterface can be created. Once created, the method LoadJVM needs to be called with the JVM parameters, and this will then load up the Java Virtual Machine. Once loaded, the user calls the method to instantiate the Java object (note that the use of the method InstantiateJavaObject is optional as the user may just want to call a static method, in which case, this method does not need to be called; however, it will not case any harm).

    Java = new JavaNativeInterface();
    Java.LoadVM(jvmParameters, false);
    Java.InstantiateJavaObject(Name of the java class excluding the extension);
    

    Once the JVM has been loaded and a class instantiated, the user may call any method they wish. First create an object list that will contain all of the parameters to pass into the Java method. As it is an object list, it can hold parameters of different types as everything inherits from an object.

     List olParameters = new List();
     olParameters.Add(Value of the parameter to be passed to the java method);
    
    
    

    Next, simply call the generic method CallMethod passing in the return type of the Java method as the template type. In the example below, I am calling CallMethod which means that the return type of my Java method that I want to call is a string.

    Next, pass in the name of the Java method to call and the method descriptor (see above); finally, pass in the list of all of the parameters. (Note: If no parameters are required, then pass in an empty list.)

    Java.CallMethod("AddTwoNumbers", "(IILjava/lang/String;)I", olParameters);
    

    Well, I guess that wraps it all up, but remember that there is so much more you can do with JNI. The test application was just a quick and dirty way to demonstrate the basics of the JNI component. For a full understanding of JNI.

    Either you can get more on this link

    提交回复
    热议问题