Best way to access Java classes from C++? (better than using JNI directly)

馋奶兔 提交于 2019-12-22 08:02:01

问题


I have to integrate a large Java library in a quite large C++ application. A solution is to use JNI but this requires to hand code all the classes. Python has, for example, a wonderful solution with JPype (http://jpype.sourceforge.net/) that automatizes the process (although the same solution cannot be applied to C++ due to C++ and Python different natures).

Thanks,

Das


回答1:


SWIG is a tool that lets you auto-generate bindings from one language to another. It supports C++ and Java and a dozen other languages.




回答2:


Seems that my question was not clear enough. Maybe the confusion comes from JNI that allows the access in booth directions ...

What I want to do is to access a Java library FROM C++. That is, someone give me a JAR file that contains a collection od JAVA compiled classes and I must write code in C++ to access it (as I want to integrate the functionality of that Java library into a C++ application).

Hope that this clarifies the direction of access :)

Thanks,

Das




回答3:


Not sure if this commercial tool makes life any easier, but you might wanna explore -- http://www.teamdev.com/jniwrapper/index.jsf




回答4:


JNA is not quite what you're looking for, but it does make your life a lot easier. It doesn't require any boilerplate/generated code - you just write an interface for the methods you want to call.

Certainly, there is every reason to use JNA instead of JNI.




回答5:


I have two suggestions which may or may not work for you:

First, you could try something very simple. Run the Java code in a separate process, and communicate with it with pipes or sockets. This is fairly easy to do, and doesn't require any crazy libraries. The downside is that the communication is somewhat limited (just some simple pipes), you'll need to write your own wrapper around it to send data across

Secondly, what exactly is this library? Perhaps we can suggest alternatives that are not written in Java, that would be much easier to use in your C++ application?



来源:https://stackoverflow.com/questions/180528/best-way-to-access-java-classes-from-c-better-than-using-jni-directly

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