After use javap -p -s TryMe, does the variable appear d:
...
public static final int d;
:
...
?
, , . ? Arch x86_64, :
Test.java:
public class Test {
public int a = 1;
public final int b = 2;
public static int c = 3;
public static final int d = 4;
}
ctest.cpp:
#include <iostream>
#include <jni.h>
using namespace std;
int main(){
JavaVM *jvm;
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options[1];
options[0].optionString = const_cast<char*>("-Djava.class.path=/home/fishi/Workspace/tmp/");
vm_args.version = JNI_VERSION_1_8;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.ignoreUnrecognized = JNI_FALSE;
int res = JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);
if (res == JNI_ERR){
cout << "Fail: Unable to load JVM \t Exit" << endl;
return 1;
}
else if (res == JNI_OK){
cout << "CreateVM:\t\tJVM loaded successfully!" << endl;
}
jclass cls = env->FindClass("Test");
if(cls == NULL)
{
cout << "FindClass:\t\tCould not load class!" << endl;
return 1;
}
else
{
cout << "FindClass:\t\tLoaded class successfully!" << endl;
}
jfieldID field = env->GetStaticFieldID(cls, "d", "I");
if(field == NULL)
{
cout << "GetField:\t\tError!" << endl;
return 1;
}
cout << "Success" << endl;
}
Makefile:
build-jni:
g++ -g -I/usr/lib/jvm/java-8-openjdk/include/ -I/usr/lib/jvm/java-8-openjdk/include/linux/ -L/usr/bin/java -L/usr/lib/jvm/java-8-openjdk/jre/lib/amd64/server/ ctest.cpp -ljvm -o ctest
run-jni:
LD_LIBRARY_PATH=/usr/bin/java:/usr/lib/jvm/java-8-openjdk/jre/lib/amd64/server/ ./ctest
:
$make run-jni
LD_LIBRARY_PATH =/usr/bin/java:/usr/lib/jvm/java-8-openjdk/jre/lib/amd64/server/./ctest
CreateVM: JVM !
FindClass: !
, .