简体   繁体   English

在Raspberry Pi上从C ++代码调用Java类

[英]Calling Java class from C++ code on Raspberry Pi

I am doing a C++ program on my Raspberry Pi and I want to call a Java class from my C++ program. 我正在Raspberry Pi上执行C ++程序,并且想从C ++程序中调用Java类。

I have my main program written in C++, which is the driver for my application, however, I am trying to interface with a device through my program and the communication API is written in Java. 我的主程序是用C ++编写的,这是我的应用程序的驱动程序,但是,我试图通过我的程序与设备连接,并且通信API用Java编写。 I don't want to rewrite the entire API, so my main objective is to call the API from my C++ main. 我不想重写整个API,所以我的主要目标是从C ++ main调用API。

Does anyone know how this is possible?? 有人知道这怎么可能吗? I heard JNI is one way but I am uncertain how to accomplish this on RPI. 我听说JNI是一种方法,但是我不确定如何在RPI上完成此操作。

It can be done with JNI. 可以使用JNI完成。 As it happens I have a project on GitHub where I have been working on a C++ wrapper generator for Java. 碰巧的是,我在GitHub上有一个项目,在那里我一直在为Java开发C ++包装器生成器。

https://github.com/wshackle/java4cpp https://github.com/wshackle/java4cpp

Even if you don't want to use it, some of the code or examples might be useful to take a look at. 即使您不想使用它,查看一些代码或示例也可能很有用。

To compile a JNI program on linux ( which I think should also work for Rasberry PI although I have never used a Rasberry PI) , I would use a command of the form: 要在linux上编译JNI程序(尽管我从未使用过Rasberry PI,但我认为它也适用于Rasberry PI),我将使用以下形式的命令:

export JAVA_HOME=/usr/local/jdk1.8.0_60
g++ -I ${JAVA_HOME}/include -I ${JAVA_HOME}/include/linux -L ${JAVA_HOME}/jre/lib/i386/server/  -Wl,--rpath ${JAVA_HOME}/jre/lib/i386/server/  cpplussource.cpp -ljvm -o newprogram

I am not sure which processor the Rasberry-PI uses. 我不确定Rasberry-PI使用哪种处理器。 i386 might be replaced with something else. i386可能会替换为其他东西。 Your JDK might come with a client directory rather than a server directory. 您的JDK可能带有客户机目录而不是服务器目录。

There is more on the C++ compiler options at : 有关C ++编译器选项的更多信息,请访问:

https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/design.html#compiling_loading_and_linking_native_methods https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/design.html#compiling_loading_and_linking_native_methods

and more on invoking the virtual machine at: 有关在以下位置调用虚拟机的更多信息:

https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/invocation.html#overview https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/invocation.html#overview

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

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