eclipse下测试JNI

Step 1: Write the Java Code

Create a Java class named  HelloWorld that declares a native method. This class also includes a  main method that creates a  HelloWorld object and calls the native method.

Step 2: Compile the Java Code

Use  javac to compile the Java code that you wrote in  Step 1.

Step 3: Create the .h File

Use  javah to create a JNI-style header file (a  .h file) from the  HelloWorld class. The header file provides a function signature for the implementation of the native method  displayHelloWorld.

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 the  .h file and the  .c file that you created in  Steps 3 and 4 into 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, use java, 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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值