好的,我知道JNI是什么,但我正在寻找更容易的东西(如果有的话).
任何人都可以告诉我如何做到这一点?
任何帮助将不胜感激.
解决方法 如果您没有找到JNI’easy’,那么您需要IPC(进程间通信)机制.因此,从您的C流程中,您可以与Java流程进行通信.你在控制台重定向方面所做的是IPC的一种形式,实质上就是IPC.
由于你发送的内容的性质不是很清楚,很难给你一个很好的答案.但是如果您有“简单”的对象或“命令”可以轻松地序列化为简单的协议,那么您可以使用协议缓冲区等通信协议.
#include <iostream>#include <boost/interprocess/file_mapPing.hpp>// Create an IPC enabled fileconst int fileSize = 1000;std::filebuf fbuf;fbuf.open("cpp.out",std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::binary); // Set the sizefbuf.pubseekoff(fileSize-1,std::ios_base::beg);fbuf.sputc(0);// use boost IPC to use the file as a memory mapped regionnamespace ipc = boost::interprocess;ipc::file_mapPing out("cpp.out",ipc::read_write);// Map the whole file with read-write permissions in this processipc::mapped_region region(out,ipc::read_write);// Get the address of the mapped regionvoID * addr = region.get_address();std::size_t size = region.get_size();// Write to the memory 0x01std::memset(addr,0x01,size);out.flush();
现在你的java文件可以打开’cpp.out’并像普通文件一样读取内容.
总结以上是内存溢出为你收集整理的C和Java程序之间的交互/通信全部内容,希望文章能够帮你解决C和Java程序之间的交互/通信所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)