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
|
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 #ifndef WIN32
30 #include "pthread.h"
31 #endif
32
33 static jclass classClass;
34 static jclass iaeClass;
35 static jmethodID mid_Class_forName;
36 static jmethodID mid_Class_getField;
37 static jmethodID mid_Field_get;
38
39 int getField(JNIEnv *env, char* declaringClass_name, char* field_name);
40 int checkAndClearIllegalAccessExceptionThrown(JNIEnv *env);
41
42 static int main_inner(int argc, char** args) {
43 JavaVM *jvm;
44 JNIEnv *env;
45 JavaVMInitArgs vm_args;
46 JavaVMOption options[1];
47 jint rc;
48
49 vm_args.version = JNI_VERSION_1_2;
50 vm_args.nOptions = 0;
51 vm_args.options = options;
52
53 if ((rc = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args)) != JNI_OK) {
54 printf("ERROR: cannot create VM.\n");
55 exit(-1);
56 }
57
58 classClass = (*env)->FindClass(env, "java/lang/Class");
59 iaeClass = (*env)->FindClass(env, "java/lang/IllegalAccessException");
60 mid_Class_forName = (*env)->GetStaticMethodID(env, classClass, "forName",
61 "(Ljava/lang/String;)Ljava/lang/Class;");
62 assert(mid_Class_forName != NULL);
113 (*env)->NewStringUTF(env, declaringClass_name));
114 if ((*env)->ExceptionOccurred(env) != NULL) {
115 (*env)->ExceptionDescribe(env);
116 return 1;
117 }
118
119 jobject f = (*env)->CallObjectMethod(env, c, mid_Class_getField, (*env)->NewStringUTF(env, field_name));
120 if ((*env)->ExceptionOccurred(env) != NULL) {
121 (*env)->ExceptionDescribe(env);
122 return 2;
123 }
124
125 jobject v = (*env)->CallObjectMethod(env, f, mid_Field_get, c);
126 if ((*env)->ExceptionOccurred(env) != NULL) {
127 (*env)->ExceptionDescribe(env);
128 return 3;
129 }
130 return 0;
131 }
132
133 /* On AIX, unfortunately, we are not able to invoke the libjvm on the primordial thread. */
134
135 #ifdef _AIX
136 #define DEFAULT_SPAWN_IN_NEW_THREAD
137 #else
138 #undef DEFAULT_SPAWN_IN_NEW_THREAD
139 #endif
140
141 struct args_t {
142 int argc; char** argv;
143 };
144
145 #define STACK_SIZE 0x200000
146
147 #ifdef _WIN32
148
149 #error Not implemented on Windows.
150
151 #else
152
153 static void* thread_wrapper(void* p) {
154 const struct args_t* const p_args = (const struct args_t*) p;
155 main_inner(p_args->argc, p_args->argv);
156 return 0;
157 }
158
159 static void run_in_new_thread(const struct args_t* args) {
160 pthread_t tid;
161 pthread_attr_t attr;
162
163 pthread_attr_init(&attr);
164 pthread_attr_setstacksize(&attr, STACK_SIZE);
165
166 if (pthread_create(&tid, &attr, thread_wrapper, (void*)args) != 0) {
167 fprintf(stderr, "Failed to create main thread\n");
168 exit(2);
169 }
170
171 if (pthread_join(tid, NULL) != 0) {
172 fprintf(stderr, "Failed to join main thread\n");
173 exit(2);
174 }
175 }
176
177 #endif /* ! WIN32 */
178
179 int main(int argc, char** args) {
180 #ifdef DEFAULT_SPAWN_IN_NEW_THREAD
181 struct args_t a;
182 a.argc = argc;
183 a.argv = args;
184 run_in_new_thread(&a);
185 #else
186 main_inner(argc, args);
187 #endif
188 }
|