0

I have a native function :

int NativeFunction(std::vector<MyObject>);

I am loading the Native dll using JNA and I am trying to call this function NativeFunction from Java like:

nativedlljnapointer.NativeFunction(List<MyObject>);

I am however running into "java.lang.IllegalArgumentException: Unsupported argument type ArrayList" exception.

I al tried using java util vector when I am running into the same exception "java.lang.IllegalArgumentException: Unsupported argument type java.util.Vector"

Can someone suggest me how I could pass List from my Java function to the native function which has vector<> as an argument.

Any help will be greatly appreciated.

user3421442
  • 93
  • 1
  • 7

1 Answers1

2

std::vector and java List are completely different types, it's normal for them not to work.

Furthermore , Is MyObject a C++ defined object or a Java defined object (if you define one in each, they are again, completely different objects ! )?

The best and safest way to communicate via JNI is to use serialization, like you would between any two different environments.

Granted, it takes a bit of extra work, but in the long run you end up with more robust code.

MichaelCMS
  • 4,703
  • 2
  • 23
  • 29
  • That is probably the best method I understand. On similar lines, I have another question. Is it possible to have nested JNA calls? For eg, I make a JNA call from java to c++, c++ in-turn makes a call to Java via thrift, this java code would have to make JNA calls to the same c++ code. Is this a possible scenario? – user3421442 Aug 30 '15 at 18:04
  • You can call Java methods from C++ and C++ methods from Java (ofc. C++ from C++ and Java from Java also). So it's a possible scenario, just make sure you send the correct parameters, that the method exists, etc. Now, I don't know what thrift means for you,but the way you make Java calls from C++ is through reflection (JNI Invocation Interface) : Query the JEnv of a specific method Id, keep the pointer (if any) then call it . Check this link for a fast sample : http://www.mobileway.net/2015/03/20/android-pro-tip-call-java-methods-from-c-using-jni/ . Keep an eye out for method signatures. – MichaelCMS Sep 01 '15 at 12:41