| 1 |
#if defined(_HPUX) && !defined(_IA64) |
| 2 |
#include <dl.h> |
| 3 |
#else |
| 4 |
#include <dlfcn.h> |
| 5 |
#endif // HPUX |
| 6 |
#include <errno.h> |
| 7 |
#include <string> |
| 8 |
#include <cstring> |
| 9 |
#include <cstdio> |
| 10 |
#include <sstream> |
| 11 |
//#include <jni.h> |
| 12 |
#include "../../../native/jni_include/jni.h" |
| 13 |
|
| 14 |
void* jvmLibrary; |
| 15 |
jint(JNICALL * CreateJVM_Method)(JavaVM **pvm, void **penv, void *args); |
| 16 |
#define USE_JNI_VERSION JNI_VERSION_1_4 |
| 17 |
JavaVM *jvm; |
| 18 |
|
| 19 |
void loadLibrary(const char* path) |
| 20 |
{ |
| 21 |
#if defined(_HPUX) && !defined(_IA64) |
| 22 |
jvmLibrary = shl_load(path, BIND_DEFERRED | BIND_VERBOSE, 0L); |
| 23 |
#else |
| 24 |
jvmLibrary = dlopen(path, RTLD_LAZY | RTLD_GLOBAL); |
| 25 |
#endif // HPUX |
| 26 |
if (jvmLibrary == NULL) |
| 27 |
{ |
| 28 |
printf("Failed to load JVM from %s\n", path); |
| 29 |
printf("Errno: %d\n", errno); |
| 30 |
if (errno == ENOEXEC) |
| 31 |
{ |
| 32 |
printf("diagnostics: %s\n", dlerror()); |
| 33 |
} |
| 34 |
exit(-1); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
void unloadLibrary() |
| 39 |
{ |
| 40 |
int r = dlclose(jvmLibrary); |
| 41 |
if (r != 0) // error |
| 42 |
{ |
| 43 |
printf(" Failed to unload JVM\n"); |
| 44 |
printf("Errno: %d\n", errno); |
| 45 |
printf("Dlerror: %s\n", dlerror()); |
| 46 |
exit(-1); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
void* getSymbol(const char* name) |
| 51 |
{ |
| 52 |
void* res = dlsym(jvmLibrary, name); |
| 53 |
if (res == NULL) |
| 54 |
{ |
| 55 |
printf(" Failed to get Symbol %s\n", name); |
| 56 |
printf("errno %d\n", errno); |
| 57 |
exit(-1); |
| 58 |
} |
| 59 |
return res; |
| 60 |
} |
| 61 |
|
| 62 |
void loadEntryPoints() |
| 63 |
{ |
| 64 |
CreateJVM_Method = (jint(JNICALL *)(JavaVM **, void **, void *) )getSymbol("JNI_CreateJavaVM"); |
| 65 |
printf(" Entry point found %p\n", CreateJVM_Method); |
| 66 |
} |
| 67 |
|
| 68 |
int main(int argc, char** argv) |
| 69 |
{ |
| 70 |
if (argc < 2) |
| 71 |
{ |
| 72 |
printf("Usage: %s $JAVA_HOME/lib/server/libjvm.so [JVM_ARGS]\n", argv[0]); |
| 73 |
exit(-1); |
| 74 |
} |
| 75 |
|
| 76 |
printf("Load library\n"); |
| 77 |
loadLibrary(argv[1]); |
| 78 |
|
| 79 |
printf("Load entry points\n"); |
| 80 |
loadEntryPoints(); |
| 81 |
|
| 82 |
// Pack the arguments |
| 83 |
printf("Pack JVM arguments\n"); |
| 84 |
JavaVMInitArgs jniArgs; |
| 85 |
jniArgs.options = NULL; |
| 86 |
jniArgs.version = USE_JNI_VERSION; |
| 87 |
jniArgs.ignoreUnrecognized = false; |
| 88 |
jniArgs.nOptions = (jint) argc - 2; |
| 89 |
printf(" Num options: %d\n", jniArgs.nOptions); |
| 90 |
jniArgs.options = (JavaVMOption*) malloc(sizeof (JavaVMOption) * jniArgs.nOptions); |
| 91 |
memset(jniArgs.options, 0, sizeof (JavaVMOption) * jniArgs.nOptions); |
| 92 |
for (int i = 0; i < jniArgs.nOptions; i++) |
| 93 |
{ |
| 94 |
printf(" Option %s\n", argv[i + 2]); |
| 95 |
jniArgs.options[i].optionString = (char*) argv[i + 2]; |
| 96 |
} |
| 97 |
|
| 98 |
// Launch the JVM |
| 99 |
printf("Create JVM\n"); |
| 100 |
JNIEnv* env; |
| 101 |
jint rc = CreateJVM_Method(&jvm, (void**) &env, (void*) &jniArgs); |
| 102 |
free(jniArgs.options); |
| 103 |
if (rc != JNI_OK) |
| 104 |
{ |
| 105 |
printf(" Create JVM failed. (%d)\n", rc); |
| 106 |
exit(-1); |
| 107 |
} |
| 108 |
|
| 109 |
// Create some resources |
| 110 |
printf("Create Java resources\n"); |
| 111 |
jclass cls = env->FindClass("java/lang/Object"); |
| 112 |
jmethodID mth = env->GetMethodID(cls, "toString", "()Ljava/lang/String;"); |
| 113 |
mth = env->GetMethodID(cls, "equals", "(Ljava/lang/Object;)Z"); |
| 114 |
mth = env->GetMethodID(cls, "hashCode", "()I"); |
| 115 |
mth = env->GetMethodID(cls, "getClass", "()Ljava/lang/Class;"); |
| 116 |
|
| 117 |
|
| 118 |
// Destroy the JVM |
| 119 |
printf("Destroy JVM\n"); |
| 120 |
jvm->DestroyJavaVM(); |
| 121 |
|
| 122 |
printf("Unload library\n"); |
| 123 |
unloadLibrary(); |
| 124 |
printf("Success\n"); |
| 125 |
} |