1. program Java source file loading Native method
/** NativeDemo
* Author: huang_jc 1999/05/20
* file name: NativeDemo.java
*/
public class NativeDemo
{
int i;
int j;
public static void main(String args[])
{
NativeDemo ob = new NativeDemo();
ob.i = 10;
ob.j = ob.test();
System.out.println("this is ob.i:"+ob.i+"\n");
System.out.println("this is ob.j:"+ob.j);
}
public native int test();
static
{
System.loadLibrary("NativeDemo");
}
}
2.Compile file NativeDemo.java
Javac NativeDemo.java
3.Use javah.exe to produce file NativeDemo.c and NativeDemo.h
a : javah NativeDemo to produce NativeDemo.h which is:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <native.h>
/* Header for class NativeDemo */
#ifndef _Included_NativeDemo
#define _Included_NativeDemo
#pragma pack(4)
typedef struct ClassNativeDemo {
long i;
long j;
} ClassNativeDemo;
HandleTo(NativeDemo);
#pragma pack()
#ifdef __cplusplus
extern "C" {
#endif
extern long NativeDemo_test(struct HNativeDemo *);
#ifdef __cplusplus
}
#endif
#endif
b : use javah -stubs NativeDemo to produce NativeDemo.c which seems as:
b : use javah -stubs NativeDemo to produce NativeDemo.c which seems as:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <StubPreamble.h>
/* Stubs for class NativeDemo */
/* SYMBOL: "NativeDemo/test()I", Java_NativeDemo_test_stub */
__declspec(dllexport) stack_item *Java_NativeDemo_test_stub(stack_item *_P_,struct execenv *_EE_) {
extern long NativeDemo_test(void *);
_P_[0].i = NativeDemo_test(_P_[0].p);
return _P_ + 1;
}
4. Write your own implimentation of method test()
//file name test.c
#include <StubPreamble.h>
#include "nativedemo.h"
#include <stdio.h>
long NativeDemo_test(HNativeDemo *this)
{
printf("hello:!\n");
printf("this is in the native method!\n");
return (unhand(this)->i)*2;
}
5. link NativeDemo.c and test.c to produce NativeDemo.dll
Use VC++5.0 tools :Cl.exe
Cl /GD NativeDemo.c test.c /LD
then we get NativeDemo.dll
NativeDemo.c
test.c
Generating Code...
/out:NativeDemo.dll
/dll
/implib:NativeDemo.lib
NativeDemo.obj
test.obj
Creating library NativeDemo.lib and object NativeDemo.ex
Press any key to continue
6.Run
java NativeDemo
we get the such result:
D:\vj11user\native>java NativeDemo
hello:!
this is in the native method!
this is ob.i:10
this is ob.j:20
D:\vj11user\native>
NOTE:
Do not forget this:
c:> set INCLUDE=d:\java\include;d:\java\win32;%INCLUDE%
C:>set LIB =d:\java\lib;%LIB%
where d:\java is the directory of your jdk
new version jdk1.2.1 :
With new jdk1.2.1 :
1.javac NativeDemo.java
2. javah NativeDemo to produce head file (You need not to javah -stubs NativeDemo to produce NativeDemo.c file),
You don`t need the c file. Just the NativeDemo.h is sufficient!
3.Use VC++ produce a window dll project and add the NativeDemo.h to it.
then implement the function. All is OK!
NOTE: the data type conversion between java and c++.See jni.h for more information.
分享到:
相关推荐
通过jni完成java调用c/c++,包含c/c++生成Dll动态库
JNI开发Java和C/C++互相传递List集合, 可以参考: Java从C/C++获取List集合对象:https://blog.csdn.net/niuba123456/article/details/80994166 Java传递List集合对象到C/C++ ...
java调用C/C++全过程 JAVA以其跨平台的特性深受人们喜爱,而又正由于它的跨平台的目的,使得它和本地机器的各种内部联系变得很少,约束了它的功能。解决JAVA对本地操作的一种方法就是JNI。 JAVA通过JNI调用本地...
上篇文章中已经介绍了,关于NDK开发环境的搭建,这里不做赘述。这篇文章主要是通过一个例子来说明如何通过eclispe自动生成.h头文件,适合初学者,详见http://write.blog.csdn.net/postedit/42144847
如果你想知道怎样用java去本地调用C/C++接口,本文档应该是你需要的,特别对于无基础的朋友,无疑是很好的JNI入门指南,希望对你有帮助。
JAVA如何调用dll:用JNI调用C或C++动态联接库原来如此简单
NULL 博文链接:https://conkeyn.iteye.com/blog/1597188
JAVA通过JNI调用本地方法,而本地方法是以库文件的形式存放的(在WINDOWS平台上是DLL文件形式,在UNIX机器上是SO文件形式)。通过调用本地的库文件的内部方法,使JAVA可以实现和本地机器的紧密联系,调用系统级的...
本教程主要讲解java中如何调用C/C++,C/C++中如何调用java,并带Demo和调试指南
C/C++中访问Java方法 C/C++中访问Java父类的方法 C/C++中访问/修改Java变量 Java中访问C/C++方法 Java中访问/修改C/C++变量 动态方式实现: C/C++中访问Java方法 C/C++中访问Java父类的方法 C/C++中访问/修改Java...
安卓平台jni调用技术,通过Java代码调用C/C++代码
JNI,JNATIVE,JAWIN 使用Java调用DLL动态链接库的方案我知道的有四种:JNI,Jnative,Jawin,Jacob
java 调用 dll 的方法,即JNI的使用,demo中有get()/set()方法,操作步骤详细,即使是没用过java的程序员按照步骤依然可以成功。
V. Eclipse+CDT+MinGW 进行JAVA调用C/C++ 34 一、 安装eclipse3.2。 34 二、 安装MinGW。 34 1、 下载MinGW 34 2、 安装 34 3、 安装版本 34 4、 选择安装的编译器 34 5、 选择安装路径,下一步 35 6、 等待下载...
利用JNI技术实现Java中调用C++编写的函数库示例程序源码,并附上参考JNI文档。...详情见本人博客:Java学习之通过JNI调用C/C++编写的dll链接库(图文教程)(http://write.blog.csdn.net/postlist)
Android JNI/NDK开发(2)JNI实现C/C++与Android/JAVA相互调用 http://blog.csdn.net/u014702653/article/details/71141423
主要介绍了JNI实现最简单的JAVA调用C/C++代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
NULL 博文链接:https://wuneng94zui.iteye.com/blog/1218725
JNI,全称为Java Native Interface,即Java本地接口,JNI是Java调用Native 语言的一种特性。通过JNI可以使得Java与C/C++机型交互。即可以在Java代码中调用C/C++等语言的代码或者在C/C++代码中调用Java代码。由于JNI...
JNI,全称为Java Native Interface,即Java本地接口,JNI是Java调用Native 语言的一种特性。通过JNI可以使得Java与C/C++机型交互。即可以在Java代码中调用C/C++等语言的代码或者在C/C++代码中调用Java代码。由于JNI...