forked from DragonLi-Mi/CSharpCallJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwraper.cpp
More file actions
61 lines (60 loc) · 2.52 KB
/
wraper.cpp
File metadata and controls
61 lines (60 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <jni.h>
#include <iostream>
#include <cstdio>
using namespace std;
extern "C"{
int Invokejava(const char* message)
{
JavaVM *jvm; // Pointer to the JVM (Java Virtual Machine)
JNIEnv *env; // Pointer to native interface
//================== prepare loading of Java VM ============================
JavaVMInitArgs vm_args; // Initialization arguments
JavaVMOption* options = new JavaVMOption[1]; // JVM invocation options
options[0].optionString = "-Djava.class.path=."; // where to find java .class
vm_args.version = JNI_VERSION_1_6; // minimum Java version
vm_args.nOptions = 1; // number of options
vm_args.options = options;
vm_args.ignoreUnrecognized = false; // invalid options make the JVM init fail
//=============== load and initialize Java VM and JNI interface =============
jint rc = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); // YES !!
delete options; // we then no longer need the initialisation options.
if (rc != JNI_OK) {
// TO DO: error processing...
cin.get();
exit(EXIT_FAILURE);
}
//=============== Display JVM version =======================================
cout << "JVM load succeeded: Version ";
jint ver = env->GetVersion();
cout << ((ver>>16)&0x0f) << "."<<(ver&0x0f) << endl;
jclass cls2 = env->FindClass("MyTest");
if(cls2== nullptr){
cerr<<"ERROR : class not find";
}
else{
printf("%s",message);
cout<<"Class MyTest found"<<endl;
jmethodID mid= env->GetStaticMethodID(cls2,"sayHi","()V");
if(mid==nullptr)
cerr<<"ERROR : method void sayHi() not found!"<<endl;
else{
env->CallStaticVoidMethod(cls2,mid);
}
}
jmethodID mid2 = env->GetStaticMethodID(cls2,"Square","(I)I");
if(mid2==nullptr){
cerr<<"ERROR: method Square(int) not find!"<<endl;
}
else{
int i;
cout<<"input a number"<<endl;
cin>>i;
cout<<"get Square return = "<< env->CallStaticIntMethod(cls2,mid2,(jint)i);
cout<<endl;
}
// TO DO: add the code that will use JVM <============ (see next steps)
jvm->DestroyJavaVM();
cin.get();
return 0;
}
}