A quick (in terms of developer time) way to use a lot of C ++ code with Java

Background: We are developing a physical application that will do a lot of data analysis, but we will focus on integrating physical electronic equipment.

Basically, I would like to be able to call root (this is a large CERN data analysis library written in C ++) in the C ++ library from Java. Basically, the ability to use ROOT classes from Java (and do it without wasting a lot of time coding JNI wrappers) is showstopper to us (if this is difficult, we will most likely use Qt).

I can think of the following methods

  • JNI - as I said, we don’t want to write wrappers for each class.,
  • JNA - JNA does not provide C ++ mappings, but only C.
  • SWIG - I did not use it, but I heard that it is difficult to use.

Other things that can be reassigned: we have access to the root source code, but we do not want to change it. We want the results to be portable. We would like to stick with free libraries. And, as I said, from the very beginning we could use most of the ROOT code, without fuss.

+3
source share
9 answers

With any choice you will need to make some packaging. Although you do not want to write JNI wrappers for each class, you can write higher-level C ++ classes that span groups of methods. Then you only need to write wrappers for higher-level classes (this approach also works for other methods, and not just for JNI).

+3
source

Write a small C ++ application that reads at your input from stdin and writes the output to stdout. Then start the process from your java application and read the output from stdout.

This is the best way to do this without JNI (and it's pretty easy to do)

+6
source

I would recommend the Dropbox djinni interface creation tool . They use it for their cross-platform mobile applications to create interfaces between the Java (Android) and Objective-C (iOS) interfaces and their C ++ data model.

Facebook also used it for one of its apps. Therefore, I would suggest that it is very well tested.

See their conversation at CppCon for an overview of what he is doing. The interaction between Java and C ++ using JNI seems to be particularly error prone.

+2
source

JNIEasy supports C ++ class mapping to Java POJO classes, but it costs 399 €. Since you prefer free libraries, you can look for solutions that use something like CORBA. This is the only way to map C ++ classes to Java classes.

EDIT: did you read JAS3 , is it a java library like root?

+1
source

Just a thought, but can you use Python since Root already supports it? You could become proficient enough at the time it would take to wrap the code for Java.

0
source

Consider using C # instead of Java. If you are already familiar with java, switching to C # is easy and much better to support for calling your own code.

0
source

Whenever you call C or C ++ code with Java through JNI or its equivalent, you run the risk of destabilizing the Java platform due to memory management and / or thread safety issues on the C / C ++ side.

Before moving on to the JNI route, etc., I think you should consider other alternatives:

  • Derive Java from the equation and fully implement it in C ++ (or C ++ / C #, like someone else).
  • Create a C ++ command-line application that performs the task you need to complete using your own library, and launch the application using one of the java.lang.Runtime.exec methods.
  • Create a β€œserver” shell in C ++ for the library that provides the functionality you need as a custom protocol, and encode a conversation with the server on the Java side using HTTP, raw Sockets, Pipes, or any other transport layer.

All alternatives have disadvantages, but JNI / JNA and the like; see first paragraph.

EDIT: when you decide to use JNI / JNA in the system, there are likely to be long-term consequences. In addition to the stability problem, you must consider portability (whether working with the native library will work on Windows, Linux, etc.), create problems (it is difficult to create your own libraries in Ant, etc.), problems with platform versions (will being updated on Java 7 is something broken?), developer skills ("Joe", who did the JNI integration, is gone - who else knows Java, C ++ and JNI?). The sum of these problems (IMO) is more significant than the time required for initial development.

0
source

How about writing the classes / functions you need in C ++, compiling and calling exec () on them from java?

0
source

if it will be difficult, most likely we will use Qt

Why don't you focus on this? So far, you have not mentioned any reasons why Java should be preferred.

If the biggest part is the source of the ROOT and the code that calls it, you are probably much faster at doing all this in C ++.
Since you are fine with Qt, the user interface should not worry.

edit:
I don’t see any advantages for the Java approach - you still have to transfer most of the source to other platforms, you add complexity with the wrapper layer, and you have more dependencies.

0
source

All Articles