Is there a way to call STL libraries from JNI, I believe JNI provides a C like interface for native calls, how do we achieve this for the C++ template libraries?
有没有办法从JNI调用STL库,我相信JNI为本机调用提供了一个类似C的接口,我们如何为C ++模板库实现这一点?
4 个解决方案
#1
I agree that if you're looking for just the plain STL, you could probably use a Java library instead. However, if you insist on wrapping STL, SWIG provides some STL wrapping in JNI out of the box (see this for the basic mechanism), which should produce relatively stable, tested code.
我同意,如果你只是寻找普通的STL,你可能会使用Java库。但是,如果你坚持包装STL,SWIG会在JNI中提供一些开箱即用的STL包装(参见基本机制),这应该会生成相对稳定的测试代码。
#2
I know this is possible, although I haven't done it myself. But JNI code can crash the JVM if you are not VERY CAREFUL. Also, JNI code is much more difficult to maintain than Java code. I was on one project where the Java -> JNI -> STL and COM code was thrown away and we replaced it with C# accessing the same STL and COM and a socket. We never looked back.
我知道这是可能的,虽然我自己没有这样做。但是,如果您不是非常小心,JNI代码可能会导致JVM崩溃。此外,JNI代码比Java代码更难维护。我在一个项目中,Java - > JNI - > STL和COM代码被丢弃,我们用C#替换它,访问相同的STL和COM以及套接字。我们从未回头。
If you are doing only a small amount of JNI, it may be worth it. If you are creating a large interface via JNI, I strongly recommend instead writing the component that interacts with STL in C# instead of Java, and using a socket to communicate between the C# and Java components. It will be far easier to write, test, and maintain.
如果你只做少量的JNI,那可能是值得的。如果您是通过JNI创建一个大型接口,我强烈建议您在C#而不是Java中编写与STL交互的组件,并使用套接字在C#和Java组件之间进行通信。编写,测试和维护会容易得多。
#3
I have to ask why you find it necessary to use the STL libraries from Java. Is there something in them that's not provided by Java's own massive set of libraries?
我不得不问为什么你发现有必要使用Java中的STL库。它们中有什么东西不是由Java自己的大量库提供的吗?
I understand why you'd have to call your own code (and I'm assuming you've profiled the Java code and found it wanting, otherwise your effort is wasted) but you seem to be asking how to call STL stuff directly.
我理解为什么你必须调用你自己的代码(我假设你已经描述了Java代码并发现它想要,否则你的努力被浪费了)但你似乎在问如何直接调用STL的东西。
We've often had trouble with JNI and it makes our code less portable, so you have to have a very good reason for wanting to use it in our shop, and you have to prove with hard data, why the Java-supplied stuff is not adequate.
我们经常遇到JNI的麻烦,它使我们的代码不那么便携,所以你必须有充分的理由想在我们的商店中使用它,你必须用硬数据证明,为什么Java提供的东西是不够。
#4
Yes. You required to use the swig typemap to convert C++ STL classes to Java objects... and vice versa. For more information, please visit, http://www.swig.org/Doc2.0/Android.html
是。您需要使用swig typemap将C ++ STL类转换为Java对象...反之亦然。有关更多信息,请访问http://www.swig.org/Doc2.0/Android.html
#1
I agree that if you're looking for just the plain STL, you could probably use a Java library instead. However, if you insist on wrapping STL, SWIG provides some STL wrapping in JNI out of the box (see this for the basic mechanism), which should produce relatively stable, tested code.
我同意,如果你只是寻找普通的STL,你可能会使用Java库。但是,如果你坚持包装STL,SWIG会在JNI中提供一些开箱即用的STL包装(参见基本机制),这应该会生成相对稳定的测试代码。
#2
I know this is possible, although I haven't done it myself. But JNI code can crash the JVM if you are not VERY CAREFUL. Also, JNI code is much more difficult to maintain than Java code. I was on one project where the Java -> JNI -> STL and COM code was thrown away and we replaced it with C# accessing the same STL and COM and a socket. We never looked back.
我知道这是可能的,虽然我自己没有这样做。但是,如果您不是非常小心,JNI代码可能会导致JVM崩溃。此外,JNI代码比Java代码更难维护。我在一个项目中,Java - > JNI - > STL和COM代码被丢弃,我们用C#替换它,访问相同的STL和COM以及套接字。我们从未回头。
If you are doing only a small amount of JNI, it may be worth it. If you are creating a large interface via JNI, I strongly recommend instead writing the component that interacts with STL in C# instead of Java, and using a socket to communicate between the C# and Java components. It will be far easier to write, test, and maintain.
如果你只做少量的JNI,那可能是值得的。如果您是通过JNI创建一个大型接口,我强烈建议您在C#而不是Java中编写与STL交互的组件,并使用套接字在C#和Java组件之间进行通信。编写,测试和维护会容易得多。
#3
I have to ask why you find it necessary to use the STL libraries from Java. Is there something in them that's not provided by Java's own massive set of libraries?
我不得不问为什么你发现有必要使用Java中的STL库。它们中有什么东西不是由Java自己的大量库提供的吗?
I understand why you'd have to call your own code (and I'm assuming you've profiled the Java code and found it wanting, otherwise your effort is wasted) but you seem to be asking how to call STL stuff directly.
我理解为什么你必须调用你自己的代码(我假设你已经描述了Java代码并发现它想要,否则你的努力被浪费了)但你似乎在问如何直接调用STL的东西。
We've often had trouble with JNI and it makes our code less portable, so you have to have a very good reason for wanting to use it in our shop, and you have to prove with hard data, why the Java-supplied stuff is not adequate.
我们经常遇到JNI的麻烦,它使我们的代码不那么便携,所以你必须有充分的理由想在我们的商店中使用它,你必须用硬数据证明,为什么Java提供的东西是不够。
#4
Yes. You required to use the swig typemap to convert C++ STL classes to Java objects... and vice versa. For more information, please visit, http://www.swig.org/Doc2.0/Android.html
是。您需要使用swig typemap将C ++ STL类转换为Java对象...反之亦然。有关更多信息,请访问http://www.swig.org/Doc2.0/Android.html