Use Java JNI in Linux

Source: Internet
Author: User

 

In Linux (Ubuntu 10.04), JNI is used to call functions in the underlying. c file.

Take the simplest "hello" as an Example

 

Step 1:

Write a jnitest. Java file with the following content:

Public class jnitest

{

Static

{

System. loadlibrary ("hello ");

}

 

Public native void output ();

 

Public static void main (string [] ARGs)

{

System. Out. println (system. getproperty ("Java. Library. Path "));

Jnitest test = new jnitest ();

Test. Output ();

}

}

 

Use javac to compile. java files

Then, use javah-JNI jnitest to generate a jnitest. h header file. The content is as follows:

/* Do not edit this file-it is machine generated */

# Include <JNI. h>

/* Header for class jnitest */

 

# Ifndef _ included_jnitest

# DEFINE _ included_jnitest

# Ifdef _ cplusplus

Extern "C "{

# Endif

/*

* Class: jnitest

* Method: Output

* Signature: () V

*/

Jniexport void jnicall java_jnitest_output

(Jnienv *, jobject );

 

# Ifdef _ cplusplus

}

# Endif

# Endif

 

Note:

1. Public native void output (). This method can be considered as a method in the interface, but it is implemented by other external languages (Here C.

 

2. system. loadlibrary (string s) method is based on string S, go to Java. library to load "lib" + S + ". so "dynamic library file (DLL in Windows ).

In this way, you can access the native method.

 

3. You can use system. Out. println (system. getproperty ("Java. Library. Path"); to obtain the specific path of Java. Library, which will be used later.

 

Step 2:

Compile the jni_test.c File Based on the jnitest. h header file to implement the corresponding method.

# Include <stdio. h>

# Include "jnitest. H"

# Include <JNI. h>

 

Jniexport void jnicall java_jnitest_output (jnienv * jnienv, jobject job)

{

Printf ("Hello, ZYC! /N ");

}

 

Then, compile it into a dynamic link library in shell.

$ Gcc-shared-I/usr/lib/JVM/Java-6-sun-1.6.0.22/include/-I/usr/lib/JVM/Java-6-sun-1.6.0.22/ include/Linux/-I/usr/include/jni_test.c-O libhello. so

GCC parameter explanation (you can view it through man GCC)

-Shared: This option is used to generate a dynamic connection library. External programs cannot be connected without this flag. Equivalent to an executable file

-I include the path. Here, JNI is included in/usr/lib/JVM/Java-6-sun-1.6.0.20/include. h,/usr/lib/JVM/Java-6-sun-1.6.0.20/include/Linux contains the jni-mid.h,/usr/include/contains the user's. h file

-O file: Write the content to the file. Here it is written to libhello. So.

Then the libhello. So file is generated.

 

Step 3:

Execution method:

1. Specify the library location during a single execution

Java-djava. Library. Path =. jnitest

 

2. It will be executed through Java Library

$ Sudo CP libhello. So/usr/lib/JVM/Java-6-sun-1.6.0.22/JRE/lib/i386/client/

The following path is obtained through system. Out. println (system. getproperty ("Java. Library. Path ").

Then, you can see the result in Java jnitest and use it in eclipse.

 

Output result:

Dane-Zhu @ Dane-Zhu-desktop :~ /Workspace/javajni/src $ Java jnitest

/Usr/lib/JVM/Java-6-sun-1.6.0.22/JRE/lib/i386/client: /usr/lib/JVM/Java-6-sun-1.6.0.22/JRE/lib/i386:/usr/lib/JVM/Java-6-sun-1.6.0.22/JRE /.. /lib/i386 :. :. :. :/usr/Java/packages/lib/i386:/lib:/usr/lib

Hello, ZYC!

 

That 'OK!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.