Current location - Quotes Website - Signature design - How to create a simple JNI program HelloWorld under linux
How to create a simple JNI program HelloWorld under linux
The full name of JNI is Java Native Interface, and Java and other programming languages can call each other through JNI technology. Here we use the mutual call between Java and C. Java provides the local interface and C implements the local interface.

I use RHEL 5, mainly to test how JNI technology is realized under Linux platform. Here is an example of HelloWorld to illustrate the specific process.

First, the Java local interface Hello.java is implemented, and the code is as follows:

Class? HelloWorld? {

Public? Native? Invalid? say hello();

Static electricity {

system . loadlibrary(" hello world ");

}

Public? Static electricity Invalid? main(String[]? args)? {

(new? HelloWorld())。 say hello();

}

} where the method is declared native. In fact, the HelloWorld class is equivalent to an interface, which is declared for other programming languages. system . loadlibrary(" hello world "); This statement is a static block that is executed when the HelloWorld class is loaded. Among them, this statement realizes the loading of local dynamic link library (DLL). Under the Linux platform, the extension of DLL file is. So which is the standard object?

Compile local interface classes:

[root @ localhost JNI]# javac HelloWorld.java

Then through the compiled HelloWorld.class file, the header file of C language is generated, and the command is executed:

[root @ localhost JNI]# javah-JNI hello world

As you can see, a HelloWorld.h file has been generated in the current directory, which is the interface file of C. To realize the methods defined in the Java interface with C, you can find a method declaration in HelloWorld.h:

#ifndef? __HelloWorld__

# Definition? __HelloWorld__

# Contains? & ltjni.h & gt

#ifdef? __cplusplus

extern? " C "

{

#endif

JNIEXPORT? Invalid? JNICALL? Java_HelloWorld_sayHello? (JNIEnv? *env,? job object);

#ifdef? __cplusplus

}

#endif

#endif? /*? __HelloWorld__? */Then, implement the method in C. In the HelloWorld.c file, the code is as follows:

# Contains? & ltjni.h & gt

# Contains? " HelloWorld.h "

# Contains? & ltstdio.h & gt

JNIEXPORT? Invalid? JNICALL? Java_HelloWorld_sayHello? (JNIEnv? *env,? job object? obj)? {

Printf ("Hello, the? The world! ! !" );

} The method symbol here is Java _ Hello World _ Say Hello (JNIENV * Env, Jobject obj), and the formal parameter obj is added, otherwise it cannot be compiled.

Next, generate the dynamic connection library libHelloWorld.so and execute the command:

[root @ localhost JNI]# gcc-fPIC-shared-o libhello world . so hello world . c

You can see libHelloWorld.so in the current directory, and the name of the dynamic link library file starts with lib. Copy the file to the usr/lib directory, and you can test it.

Now execute the following command to test:

[root @ localhost JNI]# Java hello world

The output is as follows:

hello,world ! !

This is just a very simple example, mainly to understand how to use JNI under Linux. In practical application, it may be very complicated. It should be remembered that once JNI technology is used, the portability of the system will be destroyed. In some applications, it is based on this characteristic, such as restricting the spread and use of software, protecting the rights and interests of developers and so on.