20 #include "SheafSystem/jvm_launcher.h" 21 #include "SheafSystem/std_cstdlib.h" 22 #include "SheafSystem/std_iostream.h" 23 #include "SheafSystem/std_string.h" 24 #include "SheafSystem/std_unistd.h" 31 launch(
const char* main_class_name,
int argc,
char* argv[],
bool verbose)
35 char* class_path = getenv(
"CLASSPATH");
36 char* ld_library_path = getenv(
"LD_LIBRARY_PATH");
38 return launch(main_class_name, argc, argv, class_path, ld_library_path, verbose);
45 launch(
const char* main_class_name,
int argc,
char* argv[],
46 const char* xclass_path,
const char* xld_library_path,
bool verbose)
63 std::string class_path(xclass_path);
64 std::string ld_library_path(xld_library_path);
72 char* ucp = getenv(
"CLASSPATH");
75 std::string user_class_path(ucp);
76 if(class_path != user_class_path)
78 class_path +=
":" + user_class_path;
82 char* uld = getenv(
"LD_LIBRARY_PATH");
85 std::string user_ld_library_path(uld);
86 if(ld_library_path != ld_library_path)
88 ld_library_path +=
":" + user_ld_library_path;
97 JavaVMOption options[3];
99 std::string s0(
"-Djava.class.path=");
101 options[0].optionString =
const_cast<char*
>(s0.c_str());
103 std::string s1(
"-Djava.library.path=");
104 s1 += ld_library_path;
105 options[1].optionString =
const_cast<char*
>(s1.c_str());
107 options[2].optionString =
const_cast<char*
>(
"-verbose");
109 int num_options = verbose ? 3 : 2;
111 JavaVMInitArgs vm_args;
112 vm_args.version = JNI_VERSION_1_4;
113 vm_args.options = options;
114 vm_args.nOptions = num_options;
115 vm_args.ignoreUnrecognized = JNI_TRUE;
117 jint rval = JNI_CreateJavaVM(&vm, (
void**)&env, &vm_args);
120 std::cout <<
"Couldn't create the Java VM" << std::endl;
124 jclass main_class = env->FindClass(main_class_name);
127 std::cout << main_class_name <<
" class not found" << std::endl;
131 jmethodID main_method_id =
132 env->GetStaticMethodID(main_class,
"main",
"([Ljava/lang/String;)V");
134 if(main_method_id == 0)
136 std::cout <<
"main() method not found" << std::endl;
142 jclass string_class = env->FindClass(
"java/lang/String");
143 jobjectArray args = env->NewObjectArray(argc-1, string_class, NULL);
146 std::cout <<
"Out of memory" << std::endl;
154 jstring argString = env->NewStringUTF(
"");
155 env->SetObjectArrayElement(args, 0, argString);
159 for(
int i=1; i<argc; ++i)
161 jstring argString = env->NewStringUTF(argv[i]);
162 env->SetObjectArrayElement(args, i-1, argString);
168 env->CallStaticVoidMethod(main_class, main_method_id, args);
181 const char* xclass_path,
const char* xld_library_path)
199 static const char* env_name =
"__HAVE_SET_ENV_SO_LD_LIBRARY_PATH_WORKS__";
201 if(getenv(env_name) != NULL)
207 setenv(env_name,
"yes", 1);
208 setenv(
"LD_LIBRARY_PATH", xld_library_path, 1);
209 setenv(
"CLASSPATH", xclass_path, 1);
211 if(execvp(argv[0], argv))
int launch(const char *main_class_name, int argc, char *argv[], bool verbose=false)
Launch the Java virtual machine, using the current CLASSPATH and LD_LIBRARY_PATH environment variable...
void re_exec(int argc, char *argv[], const char *class_path, const char *ld_library_path)
Re-exec the current process with the using specified CLASSPATH and LD_LIBRARY_PATH environment variab...