Simple use of JNI in Ubuntu

Source: Internet
Author: User
1. Create a java file (HelloWorld. java ). ClassHelloWorld {static {System. loadLibrary ("HelloWorld");} privatenativevoidprint (); publicstaticvoidmain (Stringargs []

1. Create a java file (HelloWorld. java ).

Class HelloWorld {
Static {
System. loadLibrary ("HelloWorld ");
}
Private native void print ();
Public static void main (String args []) {
New HelloWorld (). print ();
}
 
}

2. Use the javac command to compile the java file and enter "javac HelloWorld. java" on the terminal ". Generate HelloWorld. class.

3. Use the javah command to generate the header file. Enter "javah-jni HelloWorld" in the terminal to generate HelloWorld. h

4. Use the local method to implement the method in the header file.

# Include
# Include
# Include "HelloWorld. h"
JNIEXPORT void JNICALL Java_HelloWorld_print
(JNIEnv * env, jobject obj)
{
Printf ("HelloWorld JNI! \ N ");
Return;
}

5. compile and generate the so file with gcc.

Gcc-shared-I/home/xl/java/jdk1.5.0 _ 22/include-I/home/xl/java/jdk1.5.0 _ 22/include/linux-I/usr/include HelloWorld. c-o libHelloWorld. so

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.