第一种方式,在msys中直接编译各个源文件,不管是c或者c++文件,都能生产被JNI调用的dll库
JNI-MINGW-DLL
Posted August 10th, 2008 by fhackenberger
JNI is the Java Native Interface, you will need to download and install the Java SDK. Note the installation directory (ie/ c:/j2sdk1.4.1_02 ) for use later. If you are using MSYS add a line similiar to the following in /etc/fstab and then restart MSYS:
c:/j2sdk1.4.1_02 /java
In MSYS the JNI DLL can be generated using the following (NOTE: -Wl has an 'L' not a '1'):
gcc -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at -I/java/include -I/java/include/win32 -shared -o JavaImp.dll someJavaImp.c
In a standard command console it can be generated as follows (one continuous line):
gcc -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at
-Ic:/j2sdk1.4.1_02/include -Ic:/j2sdk1.4.1_02/include/win32
-shared someJavaImp.c -o JavaImp.dll
Where JavaImp.dll should be named whatever you refer to the library as in your .java file. To use the above dll you would refer to it as follows in your java classes code:
System.loadLibrary( "JavaImp" );
If you encounter problems ensure your CLASS_PATH and PATH are set appropriately for your environment. Please refer to Java Native Interface for further details on using a JNI DLL in java code.
第二种方式, 通过JNative直接解析和调用本地dll文件中的函数
http://sourceforge.net/projects/jnative/
test.h
----------------------------------------------------------------------------------------------------------
#ifndef MINGW_DLL_H__
#define MINGW_DLL_H__
int add(int a,int b);
#endif
test.c
-----------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include "test.h"
int add(int a,int b)
{
return a+b;
}
gcc -Wall -shared test.c -o test.dll
test.java
---------------------------------------------------------------------------------------------------------------------
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;
public class Test{
public static int testJNative(int a,int b) throws NativeException, IllegalAccessException{
JNative n = null;
try{
jn = new JNative("test.dll", "add");
jn.setRetVal(Type.INT);
jn.setParameter(0 , a ) ;
jn.setParameter(1, b);
jn.invoke();
System.out.println( "return value:" + jn.getRetVal());
return Integer.parseInt(jn.getRetVal());
}finally{
if (jn != null)
jn.dispose();
}
}
public static void main(String[] args) throws NativeException, IllegalAccessException{
testJNative(1, 4);
}
}
第三种方式 这种方式只是本人的猜想,尚未验证。
由于第一种方式直接在eclipse下进行编译和链接,会导致dll文件中export的函数名称带有类似
某些后缀(@8,@16等),可以采用dlltool进行dll的一些修饰工作,把不能被JNI直接调用的dll文件进行某种修改即去掉@等后缀或者是VC的下划线等前缀,编程能直接被调用的动态链接库。
可以使用dll export view工具,查看dll文件中函数的名字以及相当地址偏移等
http://www.nirsoft.net/utils/dll_export_viewer.html
可以使用dlltool工具对dll文件的进行修饰
http://www.sourceware.org/binutils/docs-2.16/binutils/dlltool.html
mingw中使用dlltool生产动态链接库
http://blog.csai.cn/user1/265/archives/2006/3091.html
————————————————
版权声明:本文为CSDN博主「zjuylok」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zjuylok/article/details/4152864
来源:oschina
链接:https://my.oschina.net/u/4000302/blog/3274723