static void CheckForJNIException(JNIEnv* m_JVMEnv)
{
jthrowable expt = m_JVMEnv->ExceptionOccurred();
if (expt != NULL) {
m_JVMEnv->ExceptionClear();
jmethodID toString = m_JVMEnv->GetMethodID(m_JVMEnv->FindClass("java/lang/Object"), "toString", "()Ljava/lang/String;");
jstring estring = (jstring) m_JVMEnv->CallObjectMethod(expt, toString);
jboolean isCopy;
std::string message = m_JVMEnv->GetStringUTFChars(estring, &isCopy);
throw MyException("TODO", "TODO", MyString("%s", message.c_str()));
}
}
JVM::JVM(const MyArray<MyString>& class_path)
:jvm(NULL),
env(NULL)
{
/* First, try to determine version of java installed on computer */
wxRegKey jvm_runtime_environment_key(wxRegKey::HKLM, "SOFTWARE\\JavaSoft\\Java Runtime Environment");
if (jvm_runtime_environment_key.Exists() &&
jvm_runtime_environment_key.HasValue("CurrentVersion"))
{
/* Retrieve version */
wxString version;
if (jvm_runtime_environment_key.QueryRawValue("CurrentVersion", version))
{
/* Now, try to get location of client lib */
wxRegKey jvm_version_info(wxRegKey::HKLM,
MyString("SOFTWARE\\JavaSoft\\Java Runtime Environment\\%s",
MyString(version).GetCString()));
if (jvm_version_info.Exists() &&
jvm_version_info.HasValue("RuntimeLib"))
{
/* Get location of runtime */
wxString runtime_location;
if (jvm_version_info.QueryRawValue("RuntimeLib", runtime_location))
{
/* Get path */
MyString directory_path = MyFileSystemAccess::GetFileName(MyString(runtime_location)).FilePath;
/* Update Windows path */
MyString path(wxString(wxGetenv("PATH")));
path.Append(";%s", directory_path.GetCString());
wxSetEnv("PATH", path.GetCString());
/* Now, try to load dll */
if (jvm_dll.Load("jvm", wxDL_NOW))
{
/* Load all necessary symbols */
dll_ptrs.CreateJavaVM = (jint(JNICALL*)(JavaVM**,void**,void*))jvm_dll.GetSymbol("JNI_CreateJavaVM");
/* Prepare */
if (dll_ptrs.CreateJavaVM)
{
/* prepare classpath */
MyString& class_path_str("-Djava.class.path=");
for (uint32 i = 0; i < class_path.GetSize(); i++)
{
if (i > 0) class_path_str.Append(";");
class_path_str.Append(class_path.GetElement(i));
}
/* init JVM */
JavaVMInitArgs args;
JavaVMOption options[3];
args.version = JNI_VERSION_1_2;
args.nOptions = 3;
options[0].optionString = (char*)class_path_str.GetCString();
options[1].optionString = "-verbose:jni";
options[2].optionString = "-Xcheck:jni";
args.options = options;
args.ignoreUnrecognized = JNI_FALSE;
dll_ptrs.CreateJavaVM(&jvm, (void **)&env, &args);
CheckForJNIException(env);
if (jvm != NULL)
{
wxMessageBox("VM Loaded !");
return;
}
}
}
}
}
}
}
throw NoVMFound();
}
JVM::~JVM()
{
if (jvm) jvm->DestroyJavaVM();
}
MyString JVM::Invoke(const MyString& class_path, const MyString& method_name, const MyString& argument)
{
/* Correct class path, if needed */
MyString corrected_path = class_path;
corrected_path.Replace(".", "/");
jclass target_class = env->FindClass(corrected_path.GetCString());
CheckForJNIException(env);
if (target_class)
{
jmethodID target_method = env->GetStaticMethodID(target_class, method_name.GetCString(), "(Ljava/lang/String;)Ljava/lang/String;");
CheckForJNIException(env);
if (target_method)
{
jstring target_method_arg = env->NewStringUTF(argument.GetCString());
CheckForJNIException(env);
if (target_method_arg)
{
jstring result = (jstring)env->CallStaticObjectMethod(target_class, target_method, target_method_arg);
CheckForJNIException(env);
if (result)
{
return ToString(env, result);
}
}
}
}
throw MethodInvocationError();
}
MyString JVM::ToString(JNIEnv *env, jstring name)
{
const char *str= env->GetStringUTFChars(name,0);
MyString result("%s", str);
env->ReleaseStringUTFChars(name, str);
return result;
}