java jni調用 非託管 dll

來源:互聯網
上載者:User

Step1:

 編寫java類,e.g.:

//filename: HelloWorld.javapublic class HelloWorld{public native void  displayHelloWorld();public native int add(int a,int b);static {System.loadLibrary("hello");}public static void main(String[] args){HelloWorld hw=new HelloWorld();hw.displayHelloWorld();System.out.println();System.out.println(" 1+20= "+ hw.add(1,20) );}}

Step2:  javac HelloWorld.java

Step3:  javah HelloWorld

產生標頭檔如下:

/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class HelloWorld */#ifndef _Included_HelloWorld#define _Included_HelloWorld#ifdef __cplusplusextern "C" {#endif/* * Class:     HelloWorld * Method:    displayHelloWorld * Signature: ()V */JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld  (JNIEnv *, jobject);/* * Class:     HelloWorld * Method:    add * Signature: (II)I */JNIEXPORT jint JNICALL Java_HelloWorld_add  (JNIEnv *, jobject, jint, jint);#ifdef __cplusplus}#endif#endif

Step4:用vc6建立一個win32 dll項目, 項目名hello

Step5:

tools->option->directory(標籤)

directories增加兩個include 路徑(具體看java的實際include路徑):

D:\java\jdk\jdk1.6.0_21\include

D:\java\jdk\jdk1.6.0_21\include\win32

Step6:

建立hello.cpp

#include <jni.h>#include "HelloWorld.h"#include <stdio.h>#include <windows.h>JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld  (JNIEnv *, jobject){printf("hello 中國 !@#$");}/** Class:     HelloWorld* Method:    add* Signature: (II)I*/JNIEXPORT jint JNICALL Java_HelloWorld_add(JNIEnv *, jobject, jint a, jint b){return a+b;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.