欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java使用JNI调用native函数,javajninative函数,JavaWith MSV

来源: javaer 分享于  点击 35549 次 点评:249

java使用JNI调用native函数,javajninative函数,JavaWith MSV


Java

With MSVC6, create a new Win32 DLL project (simple) and call it javahowto. Inthe same directory create a java source called JavaHowTo.java

class JavaHowTo {  public native String sayHello();  static {    System.loadLibrary("javahowto");   }}

Compile the Java program and use javah utility to generate the JavaHowTo.hheader file.

javah -jni JavaHowTo

In MSVC6, add the JavaHowTo.h in your project header files In the Tools -Options menu, set the include directories to include the Java JNI headersfiles. They are located in [jdk dir]\include and [jdk dir]\include\win32directories In the javahowto.cpp source, add

#include "JavaHowTo.h" JNIEXPORT jstring JNICALL Java_JavaHowTo_sayHello  (JNIEnv *env, jobject obj) {    return  env->NewStringUTF("Hello world");}

Select the Release configuration and build the project. Copy the javahowto.dllin the same directory as the java program. Create this new java program

public class JNIJavaHowTo {  public static void main(String[] args) {    JavaHowTo jht = new JavaHowTo();    System.out.println(jht.sayHello());    }}

Compile and execute.

相关栏目:

用户点评