4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "jni.h"
28 #include "assert.h"
29
30 static jclass classClass;
31 static jclass iaeClass;
32 static jmethodID mid_Class_forName;
33 static jmethodID mid_Class_getField;
34 static jmethodID mid_Field_get;
35
36 int getField(JNIEnv *env, char* declaringClass_name, char* field_name);
37 int checkAndClearIllegalAccessExceptionThrown(JNIEnv *env);
38
39 int main(int argc, char** args) {
40 JavaVM *jvm;
41 JNIEnv *env;
42 JavaVMInitArgs vm_args;
43 JavaVMOption options[1];
44 jint rc;
45
46 vm_args.version = JNI_VERSION_1_2;
47 vm_args.nOptions = 0;
48 vm_args.options = options;
49
50 if ((rc = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args)) != JNI_OK) {
51 printf("ERROR: cannot create VM.\n");
52 exit(-1);
53 }
54
55 classClass = (*env)->FindClass(env, "java/lang/Class");
56 iaeClass = (*env)->FindClass(env, "java/lang/IllegalAccessException");
57 mid_Class_forName = (*env)->GetStaticMethodID(env, classClass, "forName",
58 "(Ljava/lang/String;)Ljava/lang/Class;");
59 assert(mid_Class_forName != NULL);
110 (*env)->NewStringUTF(env, declaringClass_name));
111 if ((*env)->ExceptionOccurred(env) != NULL) {
112 (*env)->ExceptionDescribe(env);
113 return 1;
114 }
115
116 jobject f = (*env)->CallObjectMethod(env, c, mid_Class_getField, (*env)->NewStringUTF(env, field_name));
117 if ((*env)->ExceptionOccurred(env) != NULL) {
118 (*env)->ExceptionDescribe(env);
119 return 2;
120 }
121
122 jobject v = (*env)->CallObjectMethod(env, f, mid_Field_get, c);
123 if ((*env)->ExceptionOccurred(env) != NULL) {
124 (*env)->ExceptionDescribe(env);
125 return 3;
126 }
127 return 0;
128 }
129
|
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /* On AIX, unfortunately, we are not able to invoke the libjvm on the primordial thread. */
25 #ifdef _AIX
26 #define CREATEVM_IN_NEW_THREAD
27 #else
28 #undef CREATEVM_IN_NEW_THREAD
29 #endif
30
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #include "jni.h"
35 #include "assert.h"
36
37 #ifdef CREATEVM_IN_NEW_THREAD
38 #ifdef WIN32
39 #error Not on Windows.
40 #else
41 #include "pthread.h"
42 #endif
43 #endif
44
45 static jclass classClass;
46 static jclass iaeClass;
47 static jmethodID mid_Class_forName;
48 static jmethodID mid_Class_getField;
49 static jmethodID mid_Field_get;
50
51 int getField(JNIEnv *env, char* declaringClass_name, char* field_name);
52 int checkAndClearIllegalAccessExceptionThrown(JNIEnv *env);
53
54 static int main_inner(int argc, char** args) {
55 JavaVM *jvm;
56 JNIEnv *env;
57 JavaVMInitArgs vm_args;
58 JavaVMOption options[1];
59 jint rc;
60
61 vm_args.version = JNI_VERSION_1_2;
62 vm_args.nOptions = 0;
63 vm_args.options = options;
64
65 if ((rc = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args)) != JNI_OK) {
66 printf("ERROR: cannot create VM.\n");
67 exit(-1);
68 }
69
70 classClass = (*env)->FindClass(env, "java/lang/Class");
71 iaeClass = (*env)->FindClass(env, "java/lang/IllegalAccessException");
72 mid_Class_forName = (*env)->GetStaticMethodID(env, classClass, "forName",
73 "(Ljava/lang/String;)Ljava/lang/Class;");
74 assert(mid_Class_forName != NULL);
125 (*env)->NewStringUTF(env, declaringClass_name));
126 if ((*env)->ExceptionOccurred(env) != NULL) {
127 (*env)->ExceptionDescribe(env);
128 return 1;
129 }
130
131 jobject f = (*env)->CallObjectMethod(env, c, mid_Class_getField, (*env)->NewStringUTF(env, field_name));
132 if ((*env)->ExceptionOccurred(env) != NULL) {
133 (*env)->ExceptionDescribe(env);
134 return 2;
135 }
136
137 jobject v = (*env)->CallObjectMethod(env, f, mid_Field_get, c);
138 if ((*env)->ExceptionOccurred(env) != NULL) {
139 (*env)->ExceptionDescribe(env);
140 return 3;
141 }
142 return 0;
143 }
144
145
146 #ifdef CREATEVM_IN_NEW_THREAD
147
148 struct args_t {
149 int argc; char** argv;
150 };
151
152 #define STACK_SIZE 0x200000
153
154 static void* thread_wrapper(void* p) {
155 const struct args_t* const p_args = (const struct args_t*) p;
156 main_inner(p_args->argc, p_args->argv);
157 return 0;
158 }
159
160 static void run_in_new_thread(const struct args_t* args) {
161 pthread_t tid;
162 pthread_attr_t attr;
163
164 pthread_attr_init(&attr);
165 pthread_attr_setstacksize(&attr, STACK_SIZE);
166
167 if (pthread_create(&tid, &attr, thread_wrapper, (void*)args) != 0) {
168 fprintf(stderr, "Failed to create main thread\n");
169 exit(2);
170 }
171
172 if (pthread_join(tid, NULL) != 0) {
173 fprintf(stderr, "Failed to join main thread\n");
174 exit(2);
175 }
176 }
177
178 int main(int argc, char** args) {
179 struct args_t a;
180 a.argc = argc;
181 a.argv = args;
182 run_in_new_thread(&a);
183 }
184
185 #else
186
187 int main(int argc, char** args) {
188 main_inner(argc, args);
189 }
190
191 #endif /* CREATEVM_IN_NEW_THREAD */
|