1 /* toolwrapper.c -- a native tool wrapper for VMs that support the JNI
3 Copyright (C) 2006 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
45 #ifndef JNI_VERSION_1_2
46 # error JNI version 1.2 or greater required
50 #define MAINCLASS "Main"
59 /* Typedef for JNI_CreateJavaVM dlopen call. */
60 typedef jint createVM (JavaVM **, void **, void *);
63 main (int argc, const char** argv)
68 JavaVMInitArgs vm_args;
73 jclass string_class_id;
74 jobjectArray args_array;
78 int classpath_found = 0;
79 /* Variables for JNI_CreateJavaVM dlopen call. */
80 lt_dlhandle libjvm_handle = NULL;
81 createVM* libjvm_create = NULL;
88 vm_args.options = NULL;
95 for (i = 1; i < argc; i++)
97 if (!strncmp (argv[i], "-J", 2))
99 if (!strncmp (argv[i], "-J-Djava.class.path=", 20))
102 /* A virtual machine option. */
103 vm_args.options = (JavaVMOption*) realloc (vm_args.options, (vm_args.nOptions + 1) * sizeof (JavaVMOption));
105 if (vm_args.options == NULL)
107 fprintf (stderr, TOOLNAME ": realloc failed.\n");
111 if (strlen (argv[i]) == 2)
113 fprintf (stderr, TOOLNAME ": the -J option must not be followed by a space.\n");
117 vm_args.options[vm_args.nOptions++].optionString = strdup (argv[i] + 2);
121 non_vm_argv = (char**) realloc (non_vm_argv, (non_vm_argc + 1) * sizeof (char*));
122 if (non_vm_argv == NULL)
124 fprintf (stderr, TOOLNAME ": realloc failed.\n");
127 non_vm_argv[non_vm_argc++] = strdup (argv[i]);
132 if (!classpath_found)
134 /* Set the invocation classpath. */
135 vm_args.options = (JavaVMOption*) realloc (vm_args.options, (vm_args.nOptions + 1) * sizeof (JavaVMOption));
137 if (vm_args.options == NULL)
139 fprintf (stderr, TOOLNAME ": realloc failed.\n");
143 vm_args.options[vm_args.nOptions++].optionString = "-Xbootclasspath/p:" TOOLS_ZIP;
146 /* Terminate vm_args.options with a NULL element. */
147 vm_args.options = (JavaVMOption*) realloc (vm_args.options, (vm_args.nOptions + 1) * sizeof (JavaVMOption));
148 if (vm_args.options == NULL)
150 fprintf (stderr, TOOLNAME ": realloc failed.\n");
153 vm_args.options[vm_args.nOptions].optionString = NULL;
155 /* Terminate non_vm_argv with a NULL element. */
156 non_vm_argv = (char**) realloc (non_vm_argv, (non_vm_argc + 1) * sizeof (char*));
157 if (non_vm_argv == NULL)
159 fprintf (stderr, TOOLNAME ": realloc failed.\n");
162 non_vm_argv[non_vm_argc] = NULL;
164 vm_args.version = JNI_VERSION_1_2;
165 vm_args.ignoreUnrecognized = JNI_TRUE;
167 /* dlopen libjvm.so */
168 libjvm_error = lt_dlinit ();
171 fprintf (stderr, TOOLNAME ": lt_dlinit failed.\n");
175 libjvm_handle = lt_dlopenext (LIBJVM);
178 fprintf (stderr, TOOLNAME ": failed to open " LIBJVM "\n");
181 libjvm_create = (createVM*) lt_dlsym (libjvm_handle, "JNI_CreateJavaVM");
184 fprintf (stderr, TOOLNAME ": failed to load JNI_CreateJavaVM symbol from " LIBJVM "\n");
187 result = (*libjvm_create) (&jvm, &tmp.void_env, &vm_args);
191 fprintf (stderr, TOOLNAME ": couldn't create virtual machine\n");
197 string_class_id = (*env)->FindClass (env, "java/lang/String");
198 if (string_class_id == NULL)
200 fprintf (stderr, TOOLNAME ": FindClass failed.\n");
204 args_array = (*env)->NewObjectArray (env, non_vm_argc, string_class_id, NULL);
205 if (args_array == NULL)
207 fprintf (stderr, TOOLNAME ": NewObjectArray failed.\n");
211 for (i = 0; i < non_vm_argc; i++)
213 str = (*env)->NewStringUTF (env, non_vm_argv[i]);
216 fprintf (stderr, TOOLNAME ": NewStringUTF failed.\n");
220 (*env)->SetObjectArrayElement (env, args_array, i, str);
224 = (*env)->FindClass (env,
225 "gnu/classpath/tools/" TOOLPACKAGE "/" MAINCLASS);
226 if (class_id == NULL)
228 fprintf (stderr, TOOLNAME ": FindClass failed.\n");
232 method_id = (*env)->GetStaticMethodID (env, class_id, "main", "([Ljava/lang/String;)V");
234 if (method_id == NULL)
236 fprintf (stderr, TOOLNAME ": GetStaticMethodID failed.\n");
240 (*env)->CallStaticVoidMethod (env, class_id, method_id, args_array);
246 if ((*env)->ExceptionOccurred (env))
247 (*env)->ExceptionDescribe (env);
250 (*jvm)->DestroyJavaVM (jvm);
253 /* libltdl cleanup */
256 if (lt_dlclose (libjvm_handle) != 0)
257 fprintf (stderr, TOOLNAME ": failed to close " LIBJVM "\n");
260 if (lt_dlexit () != 0)
261 fprintf (stderr, TOOLNAME ": lt_dlexit failed.\n");