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

时光怂恿深爱的人放手 提交于 2019-12-05 12:19:18

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.

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

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

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.

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?

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