Suitable for cygwin JVM (Java Virtual Machine) starters

Source: Internet
Author: User
Tags see definition

Suitable for cygwin JVM (Java Virtual Machine) starters

Introduction

Have you encountered problems with JNI and cygwin? Can't you use the JVM under elasticsearch to load the library files created under cygwin? This article will help you solve this problem by creating a JVM initiator. I suppose you have a basic understanding of JNI.

About cygwin

Cygwin is a Linux-like environment developed for the Windows platform. It consists of two parts:

1. a dll (cygwin1.dll ). As the Linux API simulation layer, it provides essential Linux API functions.

2. Linux-style tool collectors. When cygwin is used, programs compiled in Linux often do not need to be changed to Win32 programs. It can also help use Linux library functions in windows.

JNI and cygwin

Have you tried to load cygwin jni dll from Java? You may see that your application is suspended during system. loadlibrary. The reason is that the library built in cygwin depends on cygwin1.dll. If no process used by cygwin is running, dynamic loading of DLL dependent on cygwin (during loading) will cause the deadlock of the process. Therefore, we need the cygwin process to start JVM. This is simple ........! We use C language to create a starter and then compile the connection under cygwin. Cygwin's Java invocation API works and allows JVM to be started.

How to Create (start) JVM

Standard starter commands (Java or javaw.exe) in JDK or JRE are a C program connected with Java Virtual Machine (JVM. The initiator interprets the command line parameters to load the virtual machine, and then runs the Java application through the invocationg interface. In the future, JVM will become a dynamic connection library called JVM. dll.

Before going further, let's discuss several struct and functions used to create JVM.

Javavminitargs

The parameter of the Java virtual machine, which is 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_createjavavm

Jint jni_createjavavm (JavaVM ** p_vm, jnienv ** p_env, void * vm_args );

The first parameter is a pointer to the JavaVM pointer. The JavaVM structure is used to bind and detach local threads from and from the virtual machine, and destroy the Virtual Machine (the destruction of the Virtual Machine from jdk1.4 is not supported, destroyjavavm will wait until all user threads except the current thread have died before returning an error code ). The second parameter is a pointer to the jnienv pointer. The jnienv struct is the main structure for JNI programming to perform heavy work. Roughly speaking, the jnienv struct corresponds to a specific Java thread. The jnienv struct is returned by jni_createjavavm (), so it represents the master thread of the virtual machine. The third parameter is a non-type pointer, which contains the virtual machine parameters. The following code creates a VM instance:

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
}

Use this instance

To start any Java program, we must first find the specified class before calling the main () method of this class. We can also pass parameters to the Java program to be started. The following code snippet shows us the following:

// Find the class
<Code>
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 );

Use Code

The code of this starter is mainly written for use in eclipse. Currently, it can only process basic JVM parameters.

How to compile?

-> Open a cygwin shell.
-> Run the compiler. (G ++ CVM. cpp-O javaw)

How to use this starter?

* Copy this javaw.exeto the jre's bindirectory (your original javaw.exe ). Are you sure you have used this JRE in eclipse

* Start eclipse from cygwin shell. If you enter eclipse and cannot start it, it may be because the path is not set. You can make the following settings:

Export Path =/cygdrive/C/tools/eclipse3.2/: $ path

* If it can load your JNI library now, it may be because the path does not set close eclipse, and the path is set through the Export command.
E. g: Export Path =/cygdrive/C/tutorial/jni_libs/: $ path

* Start eclipse from the shell with the path you set

Note: This starter may not work with all JRE/JDK versions, because different versions have different JVM parameters. I used j2re1.4.2 _ 06 for testing and can work.

History

Initial version.


About jafarmlp

I am from tirurangadi, a village of North Kerala, India.

I like programming ...............

Click here to view jafarmlp's online profile.

Download source files-4 kbdownload JVM launcher-6 KB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.