0

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?

Nacao
  • 147
  • 3
  • 11
  • Maybe you'll find something here: http://stackoverflow.com/q/165945/2617699 and here http://stackoverflow.com/q/5900887/2617699 – JavaDM Sep 06 '13 at 08:29
  • JNI is a simple and straight way which I prefer it in your case. Also you can use Sockets(UDP/TCP) or shared-memory instead of pipes. – masoud Sep 06 '13 at 08:31

3 Answers3

2

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. (e.g. 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.

Claudiu
  • 1,469
  • 13
  • 21
0

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.

David Feurle
  • 2,687
  • 22
  • 38
0

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/

Nacao
  • 147
  • 3
  • 11