1 /* src/cacao/cacao.cpp - contains main() of cacao
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28 #if defined(ENABLE_JRE_LAYOUT)
38 #include "native/jni.hpp"
39 #include "native/native.hpp"
45 /* Defines. *******************************************************************/
47 #define LIBJVM_NAME NATIVE_LIBRARY_PREFIX"jvm"NATIVE_LIBRARY_SUFFIX
50 /* forward declarations *******************************************************/
52 static JavaVMInitArgs* prepare_options(int argc, char** argv);
55 /* main ************************************************************************
59 *******************************************************************************/
61 int main(int argc, char **argv)
63 #if defined(ENABLE_LIBJVM)
66 # if defined(ENABLE_JRE_LAYOUT)
71 #if defined(ENABLE_LIBJVM)
72 /* Variables for JNI_CreateJavaVM dlopen call. */
74 void* libjvm_VM_create;
78 bool (*VM_create)(JavaVM**, void**, void*);
79 void (*vm_run)(JavaVM*, JavaVMInitArgs*);
82 // Prepare the options.
83 JavaVMInitArgs* vm_args = prepare_options(argc, argv);
85 /* load and initialize a Java VM, return a JNI interface pointer in env */
87 #if defined(ENABLE_LIBJVM)
88 # if defined(ENABLE_JRE_LAYOUT)
89 /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
91 path = (char*) os::malloc(sizeof(char) * 4096);
93 if (readlink("/proc/self/exe", path, 4095) == -1) {
94 fprintf(stderr, "main: readlink failed: %s\n", strerror(errno));
98 /* get the path of the current executable */
100 path = os::dirname(path);
101 len = os::strlen(path) + os::strlen("/../lib/"LIBJVM_NAME) + os::strlen("0");
104 fprintf(stderr, "main: libjvm name to long for buffer\n");
108 /* concatenate the library name */
110 strcat(path, "/../lib/"LIBJVM_NAME);
112 path = (char*) CACAO_LIBDIR"/"LIBJVM_NAME;
115 /* First try to open where dlopen searches, e.g. LD_LIBRARY_PATH.
116 If not found, try the absolute path. */
118 libjvm_handle = os::dlopen(LIBJVM_NAME, RTLD_NOW);
120 if (libjvm_handle == NULL) {
121 /* save the error message */
123 lterror = strdup(os::dlerror());
125 libjvm_handle = os::dlopen(path, RTLD_NOW);
127 if (libjvm_handle == NULL) {
128 /* print the first error message too */
130 fprintf(stderr, "main: os::dlopen failed: %s\n", lterror);
132 /* and now the current one */
134 fprintf(stderr, "main: os::dlopen failed: %s\n", os::dlerror());
138 // Free the error string.
139 os::free((void*) lterror);
142 libjvm_VM_create = os::dlsym(libjvm_handle, "VM_create");
144 if (libjvm_VM_create == NULL) {
145 fprintf(stderr, "main: lt_dlsym failed: %s\n", os::dlerror());
149 VM_create = (bool (*)(JavaVM**, void**, void*)) (uintptr_t) libjvm_VM_create;
152 // Create the Java VM.
154 void* env; // We use a void* instead of a JNIEnv* here to prevent a compiler warning.
156 (void) VM_create(&vm, &env, vm_args);
158 #if defined(ENABLE_LIBJVM)
159 libjvm_vm_run = os::dlsym(libjvm_handle, "vm_run");
161 if (libjvm_vm_run == NULL) {
162 fprintf(stderr, "main: os::dlsym failed: %s\n", os::dlerror());
166 vm_run = (void (*)(JavaVM*, JavaVMInitArgs*)) (uintptr_t) libjvm_vm_run;
172 // Keep compiler happy.
178 * Prepare the JavaVMInitArgs structure.
180 static JavaVMInitArgs* prepare_options(int argc, char** argv)
182 JavaVMInitArgs* vm_args;
184 vm_args = (JavaVMInitArgs*) malloc(sizeof(JavaVMInitArgs));
186 vm_args->version = JNI_VERSION_1_2;
187 vm_args->nOptions = argc - 1;
188 vm_args->options = (JavaVMOption*) malloc(sizeof(JavaVMOption) * argc);
189 vm_args->ignoreUnrecognized = JNI_FALSE;
191 for (int i = 1; i < argc; i++)
192 vm_args->options[i - 1].optionString = argv[i];
199 * These are local overrides for various environment variables in Emacs.
200 * Please do not remove this and leave it at the end of the file, where
201 * Emacs will automagically detect them.
202 * ---------------------------------------------------------------------
205 * indent-tabs-mode: t