Cygwin suited to the JVM (java virtual machine) Starter

  Cygwin suited to the JVM (java virtual machine) Starter 

  Introduction 

  JNI Cygwin and you encounter problems?    You use the JVM Windoes load Cygwin not created under the document?    This article will help you through the creation of JVM starter to solve the problem.    JNI I assume you have a basic understanding. 

  On Cygwin 

  Cygwin Windows is a platform for the development of type Linux environment.    It contains two parts: 

  1. A DLL (cygwin1.dll).    Simulation as a Linux API layer to provide a real Linux API function. 

  2.Linux style tool collector.    Cygwin use in the Linux compiler under regular procedures was almost no change in the cross into the win32 procedures.    It can also help to use Linux in the Windows library. 

  JNI and Cygwin 

  You tried java load Cygwin jni dll?    You may see your application in System.loadLibrary (). During hung.    This is because under the Build Cygwin Treasury to rely on cygwin1.dll.    If not Cygwin use in the process of operation, dynamically loaded depend on Cygwin (loading) Dll this process will lead to deadlock.    Therefore, we need to start the process of Cygwin JVM.    This simple ……..!    We use C language to create a starter and then compiled Cygwin link.    Cygwin the Java Invocation API work and allow JVM was activated. 

  How to create (activated) JVM 

  JDK or JRE standards in the start-up commands (java or javaw.exe) is a java with Virtual Machine (JVM) connecting the C procedures.    Starter explained command-line parameter loading virtual machine, and then run through invocationg interface java applications.    JVM future, it will become known as a dynamic link library jvm.dll. 

  In further, let us discuss several JVM will be used to create the structure and function 

JavaVMInitArgs

  Java virtual machine parameters, it is defined in the 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_CreateJavaVM

  Jint JNI_CreateJavaVM (JavaVM p_vm **, ** p_env JNIEnv, vm_args void *); 

  The first parameter is a pointer pointing javaVM pointer.    JavaVM physical structure being used to bind and separate from the virtual machine in the local thread, but also to the destruction of this virtual machine (from the beginning of the destruction of JDK1.4 virtual machine approach is not supported, DestroyJavaVM will have to wait until the addition to the current line - there are threads all users will return to the death after a mistake).    The second parameter is a pointer pointing JNIEnv indicator.    JNI JNIEnv structure is the main programming do heavy work of the structure.    Rough speaking, JNIEnv structure with a specific java threads Correspondingly, the JNIEnv structure from JNI_CreateJavaVM (), it represents the main thread of the virtual machine.    The third parameter is a non-pointer type, which includes virtual machine parameters.    Below is a creation of the virtual machine code examples: 

  JavaVMInitArgs vm_args; 
  JavaVMOption options [4]; 

  / * Disable JIT * / 

  Options [0]. OptionString = "NONE-Djava.compiler ="; 

  / * 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 **, ** env void, 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 
  ) 


  Use this example 

  In order to be able to get a java arbitrary procedures, we must first find our designated categories before we can call this kind of main () method.    We can also pass parameters to java we will be launching procedures.    The following code fragment showing us these: 

  / / Find the class 
<code>
  Jclass jcJclass = psJNIEnv-> FindClass (mainClassName); 

  / / Find the main method id 

  JmethodID jmMainMethod = psJNIEnv-> GetStaticMethodID (jcJclass, "main" and "([Ljava / lang / String;) V"); 

  / / Call the main method. 

  PsJNIEnv-> CallStaticVoidMethod (jcJclass, jmMainMethod, joApplicationArgs); 

  Use code 

  The start of this code is primarily for use in writing eclipse, it now can only handle basic JVM parameters. 

  How compiler? 

  –> Open a cygwin shell 
  –> Run compiler (g + + cvm.cpp-o javaw) 

  How to use this starter? 

  * Copy this to your javaw.exe JRE used by the bin directory (backup your original javaw.exe).    In the eclipse that you use this JRE 


  * Cygwin shell launched from the eclipse.    If you enter eclipse, could not be activated, possibly because PATH is not provided, you can do the following settings 

  Export PATH = / cygdrive/c/tools/eclipse3.2 /: $ PATH 

  * Now you can not load the JNI library, probably because it does not set PATH Close eclipse, the path set up by export orders 
  Eg: export PATH = / cygdrive / c / tutorial / jni_libs /: $ PATH 

  * Set up a path from your shell of the start of the eclipse 

  Note: This may not be the starter for all JRE / JDK versions of the work, because different versions of the JVM parameters are different.    I used j2re1.4.2_06 do the test, can work. 

  Http://www.codeproject.com/useritems/cvm/cvm_src.zip 
  Http://www.codeproject.com/useritems/cvm/cvm.zip 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags:

Releated Java Articles

Comments

Leave a Reply