Guy Kogus Guy Kogus - 16 days ago 12
C++ Question

C++ compilation error for JNI: unknown type name JNIEnv jint JavaVM

I'm getting a compilation error for the following header file:

#include <jni.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
jint x1;
jint y1;
jint x2;
jint y2;
} Bounds;

...

#ifdef __cplusplus
};
#endif


There are other JNI references, such as to
jobject
,
JNIEnv
,
JavaVM
, etc.
It's not complaining that the <jni.h> header is missing (it was, but that was easily fixed by adding the include path). I've checked the header file and the types are defined in that header (and <jni_md.h> too).

This isn't making any sense to me. Any ideas?

EDIT: I forgot to include the following error text.

g++ -O2 -fPIC -fpermissive -I. -I.. -I/usr/include -I/usr/local/include/libavcodec -I/usr/local/include/libavdevice -I/usr/local/include/libavformat -I/usr/local/include/libswscale -I/System/Library/Frameworks/JavaVM.framework/Versions/A/Headers -DUNIX -shared -c -o Plugin.o Plugin.cpp
clang: warning: argument unused during compilation: '-shared'
In file included from Plugin.cpp:19:
In file included from Plugin.h:16:
Data.h:24:5: error: unknown type name 'jint'
jint x1;
^
Data.h:25:5: error: unknown type name 'jint'
jint y1;
^
Data.h:26:5: error: unknown type name 'jint'
jint x2;
^
Data.h:27:5: error: unknown type name 'jint'
jint y2;
^

Answer

The C code does not have any obvious error and it is compilable if the development environment is correctly set up. So the suspected area is the development environment and that is probably missing or corrupted JNI header files.

The C compiler provides the option -E which and compiler runs preprocessor only when the option is applied. The output might be analyzed contains locations where the header files was found expanded ifdefs and so on.

The preprocessor output shows the wrong jni.h file was included. The solution is to properly setup the project include paths the include the correct jni.h.