0

I have my JNI header file implementation as shown below

apptester.java:

public native boolean bool1(Vector<Byte>filepath);

The reason I declared it as Byte because according to javadocs, it says 8-bit int can be declared as byte.

byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.

This is my c++ code:

JNIEXPORT jboolean JNICALL Java_app_Tester_bool1
(JNIEnv*, jobject, jbyteArrayfilepath) {
    //codes...
    myProxy->bool1(filepath, callStatus, retcode);
    //codes...
    if (retcode == 0) {
        status = true;
        return status;
    } else {
        return status;
    }
}

Now, in my c++ editor it shows error something like this -> no suitable constructor exists to convert from "jobject" to "std::vector<uint8_t, std::allocator<uint8_t>>

Originally the c++ code is expecting std::vector<uint8_t>filepath;.

I am not really sure how to convert jobject to vector type. Are there anyone who can help me out with this error.

Botje
  • 26,269
  • 3
  • 31
  • 41
Raaj Lokanathan
  • 479
  • 3
  • 7
  • 19
  • First things first: you're confusing the primitive type `byte` with the full-blown object `Byte`. Do you really need a `Vector` on the Java side? Because a `byte[]` or direct `ByteBuffer` is far easier to work with in JNI. – Botje Sep 29 '20 at 06:50
  • Secondly, you cannot directly convert a `jobject` to a c++ `std::vector`. You will need some transformation code to map/copy the Java array into a C++ pointer and go from there. Stackoverflow already has questions about that. – Botje Sep 29 '20 at 06:56
  • @Botje as you mentioned, I realized that byte[] is far simpler to work with JNI. I have already made the changes to my JNI code. But secondly, I am not able to find questions in the Stackoverflow which are related. Could you help me out please. – Raaj Lokanathan Sep 29 '20 at 07:03
  • Does this answer your question? [How to convert the contents of a Java byte array to C string in JNI?](https://stackoverflow.com/questions/38768615/how-to-convert-the-contents-of-a-java-byte-array-to-c-string-in-jni) or [Can a Java array be passed to a C/C++ function which takes an array?](https://stackoverflow.com/questions/40419466/can-a-java-array-be-passed-to-a-c-c-function-which-takes-an-array) – Botje Sep 29 '20 at 07:41
  • Use `GetByteArrayElements` and feed the result into `std::vector`'s constructor. Then call `ReleaseByteArrayElements` with `JNI_ABORT` as the mode. – Michael Sep 29 '20 at 07:41

0 Answers0