Test JNI in eclipse

Source: Internet
Author: User
Step 1: Write the Java code

Create a Java class namedHelloWorldThat declares a native method. This class also includesmainMethod that createsHelloWorldObject and callthe native method.

Step 2: Compile the Java code

UsejavacTo compile the Java code that you wrote inStep 1.

Step 3: Create the. h file

UsejavahTo create a JNI-style header file (.hFile) fromHelloWorldClass. the header file provides a function signature for the implementation of the native
MethoddisplayHelloWorld.

Step 4: Write the native method implementation

Write the implementation for the native method in a native language (such as ansi c) source file. The implementation will be a regular function that's integrated with your Java class.

Step 5: create a shared library

Use the C compiler to compile.hFile and.cFile that you created inSteps 3 and 4Into a shared library. in Windows 95/NT terminology, a shared library
Is called a dynamically loadable library (DLL ).

Step 6: run the program

And finally, usejava, The Java interpreter, to run the program.

Package JNI;

Public class helloworld {

Public native void displayhelloworld ();

Static {
// System. loadlibrary ("hello ");
Runtime. getruntime (). loadlibrary ("hello ");
}

Public static void main (string [] ARGs ){
New helloworld (). displayhelloworld ();
}

}

# Include <JNI. h>
# Include "jni_helloworld.h"
# Include <stdio. h>

Jniexport void jnicall
Java_jni_helloworld_displayhelloworld (jnienv * ENV, jobject OBJ)
{
Printf ("Hello world! \ N ");
Return;
}

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.