Search code examples
c++cjava-native-interfacejvmti

'variable JNIEXPORT is not type name'


I don't know why is giving me errors on JNIEXPORT and JNICALL...

Info: Visual studio 2017, Windows

JAVA_HOME is set.

Added the directory in 'C/C++ > General > Additional Include Directories'

Include directory > C:\Program Files\Java\jdk1.8.0_251\include

I tried adding jvm.lib but didn't work.

error: JNIERROR - 'variable JNIEXPORT is not type name' JNICALL - 'Expected ;'

code:

#include <iostream>
#include <jvmti.h>
#include <jni.h>


JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved) {

}

Solution

  • When you include "jni.h" to compile JNI code in C or C++ you have to add both your JDK's "include" directory to your include path and the directory where the platform dependent include files (referenced by "jni.h") are. Those are "include\win32" on Windows, "include/linux" on Linux, and "include/darwin" on macOS. Usually a compiler will output a message if an include file is not found, so pay attention to all messages, not only the last one.