The decision to use JNI is actually to be able to communicate and call the lucene index in the background. Therefore, the boss decided to use this method to implement the index distributed index Service on multiple machines. After receiving the task, C ++ is used to call the Lucene java query encapsulation class.
I have used Java and C ++ for a period of time, but I have never touched JNI. I started to collect this information from the Internet and was particularly depressed when I had no guidance from the beginning. Most of the information I found on the Internet is about how to use Java to call C ++, in addition, I try to provide a dynamic library for Java in DLL mode in windows. Java is the host, but what I want is C ++ as the host and Linux environment. For a long time, various environment settings, header file searches, library designation, and other things are often overwhelmed! After a day, I finally figured out how to call the Virtual Machine (crazy sweat !!!), In another day, I finally figured out how to call classes and functions in Java. Basically, you can start writing JNI programs. :) now you can record your experiences as follows: First, let's get to know what JNI is doing. Java Native Interface (JNI) is a local programming interface in Java and is part of j2sdk. In Java programs, we can use JNI to implement some functions that are inconvenient to implement in Java. JNI is usually used in the following situations. You want to use some existing class libraries or applications, but some of the programs that are not written in Java have strict speed requirements, you choose to use assembler or C language to implement and call them in Java. In addition, many local programs are used to use Java classes. That is to say, JNI is an interface for interaction with everyone. It has become part of the Java platform as a standard and allows Java code to interact with code written in other languages. In any case, JNI is a specification that specifies virtual machine interfaces and leaves specific implementations to developers. My environment, that is, the condition in this article, is: Linux RH EL4, installed with JDK 1.5.0. To use JVM to call Java classes, you must first enable the Java Virtual Machine. How to start it? How to call C ++? First, we need to find the header file of JNI, which is generally a directory containing Java, such as JNI in (/usr/Java/jdk1.5.0/include. h. in Linux, remember to add/usr/Java/jdk1.5.0/include/Linux and the above two directories during G ++ compilation. Otherwise, a lot of undefined
...! Below is a piece of code in my own environment:
// Create JVM
Jnienv * Env = NULL;
JavaVM * JVM = NULL;
Bool object: beginjvm ()
...{
Javavmoption options [4];
Javavminitargs vm_args;
// Parameters
Options [0]. optionstring = "-xmx512m ";
Options [1]. optionstring = "-xms128m ";
// The directory where the Java class you need is located
Options [2]. optionstring = "-djava. Class. Path =.../lucene-core-2.0.0.jar ";
Options [3]. optionstring = "-djava. compiler = none ";
// I have not cleared this part. My JDK version is 1.5.0, but it does not seem to have jni_version_1_5.
Vm_args.version = jni_version_1_4;
Vm_args.options = options;
Vm_args.noptions = 4;
// Create a JVM and obtain the JVM and env
Int res = JNI_CreateJavaVM (& jvm, (void **) & env, & vm_args );
If (res = JNI_ERR)
...{
Printf ("Error invoking the JVM ");
Return false;
}
Return true;
}
Bool Object: EndJVM ()
...{
// Disable JVM
Jvm-> DestroyJavaVM ();
Return true;
}
Note that during compilation, add-I to specify the header file directory, and-L to specify the directory where libjvm. so is located, as shown in my:
G ++-I/usr/java/jdk1.5.0/include/-I/usr/java/jdk1.5.0/include/linux/-L/usr/java/jdk1.5.0/jre/lib/ i386/server/-o startJVM
StartJVM. cpp-ljvm
In this way, at least the java JVM can be started in C/C ++ mode. The next article will actually read and call the data and interfaces in java.
By the way, there may be some errors: first, libjvm. so: no such file or directory. Second, there are more than N undefine...
To sum up, the first is because-L is not added to indicate the path of the lib and-ljvm is added; the second is because-I is not added to indicate the header file and the header file of the linux directory.
In addition, when running, the error cannot find shared lib: libjvm. so may be reported. The problem is that run_lib_directory is not added, that is, LD_LIBRARY_PATH must be set. Command: export LD_LIBRARY_PATH =/usr/java/jdk1.5.0/jre/lib/i386/server