天下

记录修行的印记

1、Use native code through JNI (HelloWorld)

https://www.rgagnon.com/topics/java-jni.html

 

Use native code through JNI (HelloWorld)

Use native code through JNITag(s): JNI

With MSVC6, create a new Win32 DLL project (simple) and call it javahowto.

In the 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.h header 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 headers files. They are located in [jdk dir]\include and [jdk dir]\include\win32 directories
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.dll in 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.





posted on 2019-12-06 10:50 天下 阅读(278) 评论(0)  编辑 收藏 引用 所属分类: C/C++Java


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

常用链接

留言簿(4)

随笔分类(378)

随笔档案(329)

链接

最新随笔

搜索

最新评论