|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis article explains how can we create a Java Virtual Machine Launcher (like java.exe or javaw.exe). It explores how the Java Virtual Machine launches a Java application. It gives you more ideas on the JDK or JRE you are using. This launcher is very useful in Cygwin (Linux emulator) with Java Native Interface. This article assumes a basic understanding of JNI. How to Create (Launch) JVM ?The standard launcher command ( Before going further, let us discuss some of the structures and functions used to create the JVM. JavaVMInitArgsThe arguments to the Java Virtual Machine are defined in jni.h as follows: typedef struct JavaVMInitArgs {
/*JVM Version .It must be JNI_VERSION_1_2 or JNI_VERSION_1_4 or JVM will
interpret pointer as a JDK1_1InitArgs*/
jint version;
/*number of JVM options*/
jint nOptions;
JavaVMOption *options; /*see definition of JavaVMOption below*/
/*JVM option status.
if JNI_TRUE, ignore options VM does not understand
otherwise return JNI_ERR if there are any unrecognized options*/
jboolean ignoreUnrecognized;
} JavaVMInitArgs;
/*Definition of JavaVMOption*/
typedef struct JavaVMOption {
char *optionString; /*a string containing the argument*/
/*extra info to the JVM.Not important.*/
void *extraInfo;
} JavaVMOption;
JNI_CreateJavaVMjint JNI_CreateJavaVM(JavaVM **p_vm, JNIEnv **p_env, void *vm_args);
The first argument is a pointer to a JavaVMInitArgs vm_args;
JavaVMOption options[4];
/* disable JIT */
options[0].optionString ="-Djava.compiler=NONE";
/* user classes */
options[1].optionString = "-Djava.class.path=c:\\myclasses";
/* native lib path */
options[2].optionString = "-Djava.library.path=c:\\mylibs";
options[3].optionString = "-verbose:jni"; /* print JNI msgs */
vm_args.version = JNI_VERSION_1_2;
vm_args.options = options;
vm_args.nOptions = 4;
vm_args.ignoreUnrecognized = TRUE;
//Pointer to the function JNI_CreateJavaVM
typedef jint (JNICALL CreateJavaVM_t)(JavaVM **pvm, void **env, void *args);
//Load the jvm DLL (the jvm !)
HINSTANCE hinst = LoadLibrary("jvm.dll")
//Get the address of the function
CreateJavaVM_t *pfnCreateJavaVM = GetProcAddress(hinst, "JNI_CreateJavaVM");
//Create JVM
Int iRetval = pfnCreateJavaVM((&vm, (void **)&env,
&vm_args);
//Error handling.
if (res < 0) {
... /* error occurred
}
Invoking the ClassTo launch any Java program, first we have to find out the class we specified and then we can call the main method of that class. We can pass arguments also to the Java program we are launching. The following piece of code illustrates this: //Find the class
jclass jcJclass = psJNIEnv->FindClass(mainClassName);
//Find the main method id
jmethodID jmMainMethod =
psJNIEnv-> GetStaticMethodID(jcJclass, "main", "([Ljava/lang/String;)V");
//Call the main method.
psJNIEnv->CallStaticVoidMethod(jcJclass, jmMainMethod, joApplicationArgs);
Where Can I Use this Launcher?Have you ever tried to load the libraries which are compiled under cygwin from Java? You can see that your application hangs during About CygwinCygwin is a Linux-like environment for Windows. It consists of two parts:
Using the CodeThe code for this launcher is written mainly to work with eclipse. Now it handles only basic JVM arguments. How to Compile the Code->Open a cygwin shell ->Run the compiler. ( How to Use the Launcher
Note: This launcher may not work with all JRE/JDK versions, because, the arguments to the JVM differ from version to version. I tested with j2re1.4.2_06 and it works.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||