Java calls the so dynamic link library

Source: Internet
Author: User

Implement with JNI
Instance:

Create helloworld. Java
Class helloworld
{
Private native void print ();
Public staticvoid main (string [] ARGs)
{
New helloworld (). Print ();
}

Static
{
System. loadlibrary ("helloworld ");
}
}
Note the declaration of the print method. The keyword native indicates that the method is a nativeCode. In addition, pay attention to the call to system. loadlibrary of the static code segment.ProgramWhen loading, the libhelloworld. So library is automatically loaded.
Compile helloworld. Java
Run the following command in the command line:
Javac helloworld. Java
Compile and generate helloworld. Class in the current folder.
Generate helloworld. h
Run the following command in the command line:
Javah-JNI helloworld
Helloworld. H is generated in the current folder. Open helloworld. h and you will find the following code:
/* Do not edit this file-it is machine generated */
# Include <JNI. h>
/* Header for class helloworld */

# Ifndef _ included_helloworld
# DEFINE _ included_helloworld
# Ifdef _ cplusplus
Extern "C "{
# Endif
/*
* Class: helloworld
* Method: Print
* Signature: () V
*/
Jniexport void jnicall java_helloworld_print
(Jnienv *, jobject );

# Ifdef _ cplusplus
}
# Endif
# Endif
This file contains a declaration of the java_helloworld_print function. Here there are two parameters, which are very important and will be discussed later.
Implement helloworld. c
Enter the following code to create the helloworld. c file:
# Include <JNI. h>
# Include <stdio. h>
# Include "helloworld. H"

Jniexport void jnicall

Java_helloworld_print (jnienv * ENV, jobject OBJ)
{
Printf ("Hello world! \ N ");
}
Note that the JNI. h header file must be included. This file defines various types and macro definitions used by JNI.
In addition, pay attention to the two parameters of java_helloworld_print. This example is simple and does not require these two parameters. However, these two parameters are very important in JNI.
Env represents the Java Virtual Machine environment. The parameters passed by Java are very different from those passed by C. You need to call the interface provided by JVM to convert it to C type, the env method is called to complete the conversion.
OBJ indicates the called object, which is equivalent to this of C ++. When the C function needs to change the member variable of the called object, this object can be operated.
Compile and generate libhelloworld. So
Run the following command in Linux to complete compilation:
CC-I/usr/lib/JVM/Java-6-sun/include/Linux/
-I/usr/lib/JVM/Java-6-sun/include/
-FPIC-shared-O libhelloworld. So helloworld. c

Or (I compiled it like this)
Gcc-I $ java_home/include/Linux-FPIC-shared-O libhelloworld. So helloworld.C

Generate libhelloworld. So in the current directory. Note that the include directory of Java must be included (set according to your system environment), because helloworld. C contains JNI. h.
In helloworld. Java, the loadlibrary method loads
"helloworld", but the generated library is libhelloworld. This is stipulated by the Linux link. A library must be lib + Library
name +. So. You only need to provide the database name for the link.
run the Java program helloworld.
The last step is to verify the previous result.
JAVA helloworld
if a problem occurs in this step, if you receive Java. lang. unsatisfiedlinkerror is abnormal. You can specify the path of the shared library as follows:
JAVA-djava. library. path = '. 'helloworld
Of course, there are other ways to specify the path. For details, refer to using JNI on Linux.
we can see the long-overdue "Hello world !" Output.

Related Article

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.