FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java4cpp/src/main/resources/javamain.cpp at java4cpp-1.2 · wshackle/java4cpp · GitHub
wshackle
/
java4cpp
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
9
Code
Issues
1
Pull requests
1
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
java4cpp
/
src
/
main
/
resources
/
javamain.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
141 lines (122 loc) · 3.59 KB
Breadcrumbs
java4cpp
/
src
/
main
/
resources
/
javamain.cpp
Copy path
File metadata and controls
141 lines (122 loc) · 3.59 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* File: main.cpp
* Author: Will Shackleford {@literal <william.shackleford@nist.gov>}
*
* Created on July 30, 2015, 10:03 AM
*/
#
include
<
cstdlib
>
#
include
<
iostream
>
#
include
"
javamain.h
"
#
include
<
jni.h
>
using
namespace
std
;
using
namespace
javamain
;
static
JNIEnv *
getNewEnv
() {
JavaVM *jvm;
/*
denotes a Java VM
*/
JNIEnv *env;
/*
pointer to native method interface
*/
JavaVM * jvmBuf[
1
];
jsize nVMs;
jint v =
JNI_GetCreatedJavaVMs
(jvmBuf,
1
, &nVMs);
if
(nVMs >
0
) {
jvmBuf[
0
]->
GetEnv
((
void
**) &env,
JNI_VERSION_1_6
);
return
env;
}
JavaVMInitArgs vm_args;
/*
JDK/JRE 6 VM initialization arguments
*/
JavaVMOption* options =
new
JavaVMOption[
1
];
options[
0
].
optionString
= (
char
*)
getenv
(
"
JVM_OPTIONS
"
);
vm_args.
version
=
JNI_VERSION_1_6
;
vm_args.
nOptions
=
NULL
!= options[
0
].
optionString
?
1
:
0
;
vm_args.
options
= options;
vm_args.
ignoreUnrecognized
=
false
;
/*
load and initialize a Java VM, return a JNI interface
* pointer in env
*/
JNI_CreateJavaVM
(&jvm,
((
void
**) (&env)),
((
void
*) (&vm_args)));
delete
options;
return
env;
}
static
inline
jclass
getMainClass
();
static
inline
JNIEnv *
getEnv
();
static
jclass
getNewMainClass
() {
jclass clss =
getEnv
()->
FindClass
(
"
Main
"
);
if
(
NULL
== clss) {
cerr <<
"
Can't find class Main
"
<< endl;
}
return
clss;
}
namespace
javamain
{
void
Main::staticTest
(jint i) {
JNIEnv *env =
getEnv
();
jclass cls =
getMainClass
();
if
(cls !=
NULL
) {
jmethodID mid = env->
GetStaticMethodID
(cls,
"
staticTest
"
,
"
(I)V
"
);
if
(
NULL
== mid) {
std::cerr <<
"
Class Main has no method named staticTest
"
<< std::endl;
}
else
{
env->
CallStaticVoidMethod
(cls, mid, i);
}
}
}
Main::Main
(jint i) {
JNIEnv *env =
getEnv
();
jclass cls =
getMainClass
();
if
(cls !=
NULL
) {
jmethodID mid = env->
GetMethodID
(cls,
"
<init>
"
,
"
(I)V
"
);
if
(
NULL
== mid) {
std::cerr <<
"
Class Main has no method constructor with int parameter
"
<< std::endl;
}
else
{
jthis = env->
NewObject
(cls, mid, i);
jobjectRefType ref = env->
GetObjectRefType
(jthis);
if
(ref != JNIGlobalRefType) {
jthis = env->
NewGlobalRef
(jthis);
}
}
}
}
jint
Main::getI
() {
jclass cls =
getMainClass
();
JNIEnv *env =
getEnv
();
if
(cls !=
NULL
) {
jmethodID mid = env->
GetMethodID
(cls,
"
getI
"
,
"
()I
"
);
if
(
NULL
== mid) {
std::cerr <<
"
Class Main has no method named getI
"
<< std::endl;
return
(jint) -
1
;
}
else
{
return
env->
CallIntMethod
(jthis, mid);
}
}
}
Main::~Main
() {
if
(
NULL
!= jthis) {
getEnv
()->
DeleteGlobalRef
(jthis);
jthis=
NULL
;
}
}
}
static
JNIEnv *env =
NULL
;
static
jclass mainClass =
NULL
;
namespace
javamain
{
void
closeJVM
() {
JavaVM * jvmBuf[
1
];
jsize nVMs;
env =
NULL
;
mainClass =
NULL
;
jint v =
JNI_GetCreatedJavaVMs
(jvmBuf,
1
, &nVMs);
if
(nVMs >
0
) {
jvmBuf[
0
]->
DestroyJavaVM
();
}
}
}
static
inline
jclass
getMainClass
() {
if
(mainClass !=
NULL
) {
return
mainClass;
}
mainClass =
getNewMainClass
();
}
static
inline
JNIEnv *
getEnv
() {
if
(env !=
NULL
) {
return
env;
}
env =
getNewEnv
();
return
env;
}
Back
|
FazBrowse Home
|
New Git URL