接收从java到C ++的回调,并通过JNI在C ++中捕获java异常 [英] Receiving callbacks from java into C++ and catching java exceptions in C++ through JNI

查看:141
本文介绍了接收从java到C ++的回调,并通过JNI在C ++中捕获java异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在C ++应用程序中通过JNI使用了一个java库(.JAR),因此我们可以从C ++调用Java类方法。但是,我们还希望将Java方法抛出的异常捕获到调用Java方法的C ++代码中。如何使用JNI执行此操作?



我们还面临的另一个挑战是,我们还希望从Java库接收回调到C ++中。具体来说我们想要做的是 - Java中有一个接口,这意味着它只有方法定义,没有实现。现在,我们希望通过JNI在C ++中实现Java的这个接口,所以接口的方法将用C ++代码实现,然后实现这个接口的C ++类的对象应作为参数发送到方法调用中。 C ++到Java,所以这就像从C ++到Java的回调类的注册。这样,只要Java代码需要向C ++代码发送通知,它就可以调用C ++注册类的方法。如何在JNI实现这一目标?



我尝试过:



我不能尝试,但有可能吗?

We have a java library (.JAR) that we are consuming in C++ application through JNI, so we are able to call Java class methods from C++. However, we would also like to catch exceptions thrown by the Java methods into our C++ code that is calling the Java methods. How to do this using JNI?

The other challenge we have is we would also like to receive callbacks from Java library into C++. Specifically what we would like to do is - There is an interface in Java, meaning it only has method definitions, no implementation. Now, we want to implement this interface of Java in C++ through JNI, so the methods of the interface will be implemented in C++ code and then the object of the C++ class that implements this interface should be sent as a parameter in a method call from C++ to Java, so this is like registration of callback class from C++ to Java. This way, whenever the Java code needs to send notifications to C++ code, it can call the methods of the C++ registered class. How to achieve this in JNI?

What I have tried:

I can not try but it is possible or not?

推荐答案

参见 Java技巧10:用Java实现回调例程JavaWorld [ ^ ],这可能会有所帮助。
See Java Tip 10: Implement callback routines in Java | JavaWorld[^], which may help.


这篇关于接收从java到C ++的回调,并通过JNI在C ++中捕获java异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆