Fast (in means of developer time) way to use a lot of C++ code from Java

穿精又带淫゛_ 提交于 2019-12-12 13:49:24

问题


Background: We are designing a physics app, that will do a lot of data analysis, but our focus is integrating physical electronic equipement.

Basically I would like to be able to call root (it's a big data analysis library from CERN written in C++) library written in C++ library from Java. Basically ability to use ROOT classes from Java (and doing it without losing much time to code JNI wrappers) is a showstopper to us (if it will be hard, most probably we will be using Qt).

I can think of following methods

  • JNI - as I said - we dont want to write wrappers for every class. . .
  • JNA - JNA doesnt provide C++ mappings, but only C.
  • SWIG - I didn't use it, but heard it's hard to use.

Other things that may be revelant: we have access to root source code, but we dont want to change it. We want results to be portable. We would like to stick to free libraries. And as I said - we would be able to use much of the ROOT code from the beginning, without fuss.


回答1:


With any choice, you're going to need to do some wrapping. While you don't want to write JNI wrappers for every class, you could write higher level C++ classes that encompass groups of methods. Then you only need to write wrappers for the higher level classes (this approach works for other methods too, not just JNI).




回答2:


Write a small C++ application which reads in your input from stdin and writes an output to stdout. Then run the process from within your java app and read the output from stdout.

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




回答3:


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

Facebook used it too for one of their apps. So I'd assume, it's pretty well-tested.

See their talk at CppCon for an overview of what it does. Interfacing between Java and C++ using JNI seems especially error-prone.




回答4:


JNIEasy supports mapping of C++ classes to Java POJO classes but it costs 399€. Since you prefer free libraries you might want to look for solutions that use something like CORBA. It is the only way to have C++ classes mapped to Java classes.

EDIT: Have you considered JAS3, it is a java library similar to root?




回答5:


Just a thought but can you use Python since Root already supports it? You might well become reasonably proficient in the time it would take to wrap the code for Java.




回答6:


Consider using C# instead of Java. If you're already familiar with java, switching to C# is easy and it has much better support for invoking native code.




回答7:


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




回答8:


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

Before going down the JNI, etc route, I think you should consider other alternatives:

  • Take Java out of the equation and implement entirely in C++ (or C++ / CC# as someone else suggested).
  • Create a C++ command-line application that does the task that you need to do using the native library, and run the application using one of the java.lang.Runtime.exec methods.
  • Create a "server" wrapper in C++ for the library that exposes the functionality that you need as a custom protocol, and code the Java side to talk to the server using HTTP, raw Sockets, Pipes, or whatever transport level is appropriate.

The alternatives all have downsides, but so do JNI / JNA and the like; see the first paragraph.

EDIT: when you make the decision to use JNI / JNA in a system, there are likely to be long term consequences. In addition to the stability issue, you have to consider portability (will the native library work on Windows, Linux, etc), build issues (it is hard to build native libraries in Ant, etc), platform versions issues (will upgrading to Java 7 break something?), developer skills ("Joe" who did the JNI integration has left - who else knows Java, C++ and JNI?). The sum of these issues is (IMO) more significant than the time needed to do the initial development.




回答9:


if it will be hard, most probably we will be using Qt

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

If the biggest part is the ROOT source and the code that calls it you will be probably much faster doing it all in C++.
As you are ok with Qt, the UI shouldn't be much to worry about.

edit:
I can't really see any advantages to the Java-approach - you have to port a big part of the source to other platforms anyway, you add in complexity with the wrapping layer and you have more dependencies.



来源:https://stackoverflow.com/questions/1534734/fast-in-means-of-developer-time-way-to-use-a-lot-of-c-code-from-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!