简体   繁体   English

使用JNA从C调用Java函数

[英]Calling Java functions from C using JNA

I'm reading about Java Native Access and so far i have been able to successfully call C functions from Java. 我正在阅读Java Native Access,到目前为止,我已经能够成功地从Java调用C函数。

Is there a way to do the opposite? 有没有办法做相反的事情? Googling didnt help much. 谷歌搜索没有多大帮助。

Off course you can! 当然你可以! Let's create simple example. 让我们创建一个简单的例子。

let's create header file header.h . 让我们创建头文件header.h For callback we will use callbackTriger method. 对于回调,我们将使用callbackTriger方法。 getDeviceRandomStatus and randNum is just helper methods to generate random data responses. getDeviceRandomStatus和randNum只是生成随机数据响应的辅助方法。

#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED

typedef void(*NotificationListener)(char *, int);

void callbackTriger(const NotificationListener l);

void getDeviceRandomStatus(char *answer, int sizeOfChars);

int randNum( int min,  int max);

#endif // HEADER_H_INCLUDED

header.c header.c

#include<stdio.h>
#include "header.h"
#include <stdlib.h>
#include <time.h>

void callbackTriger(const NotificationListener l){
     int size=randNum(1,20);
     char answer[size];
     getDeviceRandomStatus(answer, size);
     (*l)(answer, sizeof(answer));
}

void getDeviceRandomStatus(char *answer, int sizeOfChars){
    int i;
    for(i=0; i<sizeOfChars; i++){
        int i=randNum(0,255);
        answer[i]=i+'0';
    }
}


int randNum( int min,  int max){
    srand ( time(NULL) );
    double scaled = (double)rand()/RAND_MAX;
    int val=(max - min +1)*scaled + min;
    return val;
}

main.c for testing library methods: main.c用于测试库方法:

#include<stdio.h>
#include <limits.h>
#include <stdlib.h>

int main(void)
{
  int sizeOfChars=randNum(1,10);
  char answer[sizeOfChars];
  getDeviceRandomStatus(answer, sizeOfChars);

  int i;
  for (i = 0; i < sizeof(answer); ++i){
       printf("%d ", answer[i]);
  }

   return 0;
}

Now lets create Shared lib and test it: 现在让我们创建共享库并测试它:

cd <path>
gcc -c -Wall -Werror -fpic header.c
gcc -shared -o libHeader.so header.o
gcc main.c -o main -lHeader -L<path> -Wl,-rpath=/home/vq/Desktop
./main

Now we need JAVA classes! 现在我们需要JAVA课程! Let's go: 我们走吧:

  import java.util.Arrays;
    import java.util.logging.Logger;

    import com.sun.jna.Callback;
    import com.sun.jna.Library;
    import com.sun.jna.Native;
    import com.sun.jna.Pointer;

    public class CallBack {

        public static Logger log = Logger.getLogger(CallBack.class.getSimpleName());

        public interface CLibrary extends Library {

            public interface NotificationListener extends Callback {
                void invoke(Pointer val, int lenth);
            }

            public static class NotificationListenerImpl implements NotificationListener {
                @Override
                public void invoke(Pointer val, int lenth) {
                    log.info("returned byte array, size: "+lenth);
                    log.info("java mehtod, callback: " +    Arrays.toString(val.getByteArray(0, lenth)));
                }
            }

            public void callbackTriger(NotificationListener callback);
        }


        static public void main(String argv[]) {

            CLibrary clib = (CLibrary) Native.loadLibrary("<path>/libHeader.so", CLibrary.class);

            // instantiate a callback wrapper instance
            CLibrary.NotificationListenerImpl callbackImpl = new CLibrary.NotificationListenerImpl();

            // pass the callback wrapper to the C library
            clib.callbackTriger(callbackImpl);


        }

    }

Looks like you can start the JVM and call functions in Java from C , using the JNI library. 看起来您可以使用JNI库启动JVM并使用Java从Java调用函数 Is this what you are after? 这就是你追求的吗?

这不适用于JNA,而是使用JNI http://en.wikipedia.org/wiki/Java_Native_Interface

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM