简体   繁体   中英

IPC between Java and C++

My goal here is to make two separate applications (one in Java and other in C++, both on the same machine) read from the same SQLite database. The C++ implementation already works and has all the methods that I need for that communication. It uses the sqlite3.h libraries.

The first rational thing to do would be to use a JDBC or a SQLite wrapper in the Java application. The problem is that my embedded system (POSIX) has very limited resources and takes very long to execute a simple query when I have included the necessary *.jar into it. I have tried out the Xerial JDBC, sqlite4java, sqljet and the Javasqlite Wrapper/JDBC driver from Christian Werner. The JavaVM just takes too long to load everything and execute it and performance is a critical issue.

As a workaround, I have managed the Java application to use system commands and run the sqlite3 command shell to execute the query and obtain the answer. I am looking for a more "stylish" and secure solution.

I actually need the Java application to use the methods from C++. They just return a string as the methods are implemented to return only one value. After a lot of IPC reading, I have reached the conclusion that I have to use named Pipes. The thing is that I would have to use JNI but I have a beginner Java level and by this time, JNI is just too complex for me. Is JNI an overkill in this case?

What other solution could I implement here?

Not sure about the performance you need over the IPC but there are several approaches:

  1. use sockets
  2. use pipes
  3. use memorymappedfiles (using memorymappedfiles you will have a performance gain)

In either case you will need a serializer/deserializer for the objects(data) you pass from java to c++ and viceversa.

Depending on the data format you might need serializer/deserializer only on Java side. (eg you send out binary data that C++ will read without needing to decode it anymore). A good tutorial on how to use memorymapped file in java can be found here and in C++ you need to use mmap function.

You could use swig . Swig can parse your C/C++ header and generate Java clases/functions of it. The generated code has jni calls to call your c++ clases or your c functions.

Actually I was wrong. I don't need to use JNI to use named pipes in Java. I have successfully communicated these two processes using basic techniques. In java I have just used FileOutputStream and FileInputStream to communicate with the named pipes.

This link was specially useful to me:

http://carminedimascio.com/2014/01/named-pipes-with-java/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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