java 调用dll动态库_java中调用 dll 动态库的简洁方法 JNative

Java中调用DLL方法,是通过JNI接口实现的,http://www.ibm.com/developerworks/cn/java/l-linux-jni/ 这里有详细的说明。大概是先用Java写一个接口类,然后用javah 生成一个xxx.h的c语言的头文件,然后用C实现这个头文件,在这个实现接口中调用其他已经实现的接口。

我看到这个实现方法后,感觉怪怪的,这不是反过来了么?随后在网上看到了JNative的说明,原来已经有人封装了这些步骤,使用JNative,你可以直接使用了,而不需要写什么接口C文件了。JNative下载后有3个文件,JNative.jar,JNativeCpp.dll以及libJNativeCpp.so,JNativeCpp.dll是用于window系统的;libJNativeCpp.so是用于linux系统的,不过这个文件是glic2.4下编译的。如果需要底版本,需要重新编译。如何编译,在后面介绍。不管什么系统,都需要把DLL文件放到系统路径下或者是加入到搜索目录。

使用说明:

JAVA中调用比较简单:

None.gifpublicString callDll(String cond)      

ExpandedBlockStart.gif

ContractedBlock.gifthrowsNativeException, IllegalAccessException...{   

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.png    JNative n=null;   

6a9c071a08f1dae2d3e1c512000eef41.png    String ret="";

6a9c071a08f1dae2d3e1c512000eef41.png    JNative.setLoggingEnabled(true); 

ExpandedSubBlockStart.gif

ContractedSubBlock.giftry...{   

6a9c071a08f1dae2d3e1c512000eef41.png        n=newJNative(dllName, methodName);//常量dllName的值为:windows下不加后缀.dll,linux需要包括后缀.so6a9c071a08f1dae2d3e1c512000eef41.pngn.setRetVal(Type.STRING);//指定返回参数的类型6a9c071a08f1dae2d3e1c512000eef41.pnginti=0;

6a9c071a08f1dae2d3e1c512000eef41.png        n.setParameter(i++, Type.STRING, cond);

6a9c071a08f1dae2d3e1c512000eef41.png        n.setParameter(i++, Type.STRING, cond);

6a9c071a08f1dae2d3e1c512000eef41.png        

6a9c071a08f1dae2d3e1c512000eef41.png        n.invoke();//调用方法6a9c071a08f1dae2d3e1c512000eef41.pngret=n.getRetVal();//取回返回值6a9c071a08f1dae2d3e1c512000eef41.png6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println("this is call dll:"+ret+"");

ExpandedSubBlockStart.gif

ContractedSubBlock.gif    }catch(Exception e)...{

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println("cann't call dll:"+dllName+"."+methodName+"");

6a9c071a08f1dae2d3e1c512000eef41.png        e.printStackTrace();

6a9c071a08f1dae2d3e1c512000eef41.png           

ExpandedSubBlockStart.gif

ContractedSubBlock.gif    }finally...{

ExpandedSubBlockStart.gif

ContractedSubBlock.gifif(n!=null)...{

ExpandedSubBlockStart.gif

ContractedSubBlock.giftry...{

6a9c071a08f1dae2d3e1c512000eef41.png            n.dispose();//记得释放ExpandedSubBlockStart.gif

ContractedSubBlock.gif}catch(Exception e)...{

6a9c071a08f1dae2d3e1c512000eef41.png                e.printStackTrace();

6a9c071a08f1dae2d3e1c512000eef41.png                System.out.println("can't dispose the dll:"+dllName);

ExpandedSubBlockEnd.gif            }ExpandedSubBlockEnd.gif        }ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.pngreturnret;

ExpandedBlockEnd.gif}

关于linux下编译 C代码部分说明:

对于linux不同版本,可能会导致libJNativeCpp.so不同

原带的libJNativeCpp.so 是在glic2.4下编译的

为了适应glic2.3的情况,重新编译了libJNativeCpp.so,在for gcc3.4.6 glibc 2.3下。

编译办法:

在JNative-1.3.2-src\JNativeCpp\Release目录下

1、备份calls.o和 asm_io.o这两个Object文件

2、make clean

3、恢复到当前目录calls.o和 asm_io.o这两个Object文件

4、make

目前已经修改了Release目录下的makefile和subdir.mk文件,使得在make clean的时候不删除calls.o和 asm_io.o两个文件

附:linux 动态库搜索路径:

export LD_LIBRARY_PATH=`pwd`:$LD_LIBRARY_PATH

makefile 文件

None.gif################################################################################

None.gif# Automatically-generated file. Do not edit!

None.gif################################################################################

None.gif

None.gif-include ../makefile.init

None.gif

None.gifRM :=rm -rf

None.gif

None.gif# All of the sources participating in the build are defined here

None.gif-include sources.mk

None.gif-include subdir.mk

None.gif-include objects.mk

None.gif

None.gififneq ($(MAKECMDGOALS),clean)

None.gififneq ($(strip $(C++_DEPS)),)

None.gif-include $(C++_DEPS)

None.gifendif

None.gififneq ($(strip $(CC_DEPS)),)

None.gif-include $(CC_DEPS)

None.gifendif

None.gififneq ($(strip $(C_DEPS)),)

None.gif-include $(C_DEPS)

None.gifendif

None.gififneq ($(strip $(CPP_DEPS)),)

None.gif-include $(CPP_DEPS)

None.gifendif

None.gififneq ($(strip $(CXX_DEPS)),)

None.gif-include $(CXX_DEPS)

None.gifendif

None.gififneq ($(strip $(C_UPPER_DEPS)),)

None.gif-include $(C_UPPER_DEPS)

None.gifendif

None.gifendif

None.gif

None.gif-include ../makefile.defs

None.gif

None.gif# Add inputs and outputs from these tool invocations to the build variables 

None.gif

None.gif# All Target

None.gifall: libJNativeCpp.so

None.gif

None.gif# Tool invocations

None.giflibJNativeCpp.so: $(OBJS) $(OBJS_ASM) $(USER_OBJS)

None.gif    @echo 'Building target: $@'

None.gif    @echo 'Invoking: GCC C++ Linker'

None.gif    g++ -shared -o"libJNativeCpp.so"$(OBJS) $(OBJS_ASM) $(USER_OBJS) $(LIBS)

None.gif    @echo 'Finished building target: $@'

None.gif    @echo ' '

None.gif

None.gif# Other Targets

None.gifclean:

None.gif    -$(RM) $(OBJS)$(C++_DEPS)$(CC_DEPS)$(C_DEPS)$(CPP_DEPS)$(LIBRARIES)$(CXX_DEPS)$(C_UPPER_DEPS) libJNativeCpp.so

None.gif    -@echo ' '

None.gif

None.gif.PHONY: all clean dependents

None.gif.SECONDARY:

None.gif

None.gif-include ../makefile.targets

None.gif

subdir.mk 文件

None.gif################################################################################

None.gif# Automatically-generated file. Do not edit!

None.gif################################################################################

None.gif

None.gif# Add inputs and outputs from these tool invocations to the build variables 

None.gifC_SRCS +=None.gif../jni_util.c 

None.gif../mem.c 

None.gif

None.gifCPP_SRCS +=None.gif../CallBack.cpp 

None.gif../WindowProcUtil.cpp 

None.gif../org_xvolks_jnative_JNative.cpp 

None.gif

None.gifASM_SRCS +=None.gif../asm_io.asm 

None.gif../calls.asm 

None.gif

None.gifOBJS +=None.gif./CallBack.o 

None.gif./WindowProcUtil.o 

None.gif./jni_util.o 

None.gif./mem.o 

None.gif./org_xvolks_jnative_JNative.o 

None.gif

None.gifOBJS_ASM +=None.gif./asm_io.o 

None.gif./calls.o

None.gif

None.gifC_DEPS +=None.gif./jni_util.d 

None.gif./mem.d 

None.gif

None.gifCPP_DEPS +=None.gif./CallBack.d 

None.gif./WindowProcUtil.d 

None.gif./org_xvolks_jnative_JNative.d 

None.gif

None.gif

None.gif# Each subdirectory must supply rules for building sources it contributes

None.gif%.o: ../%.cpp

None.gif    @echo 'Building file: $

None.gif    @echo 'Invoking: GCC C++ Compiler'

None.gif    g++ -I"/home/gongjan/jdk1.5.0_08/include/"-I"/home/gongjan/jdk1.5.0_08/include/linux"-O3 -Wall -c -fmessage-length=0-Wl,--add-stdcall-alias -MMD -MP -MF"$(@:%.o=%.d)"-MT"$(@:%.o=%.d)"-o"$@""$    @echo 'Finished building: $

None.gif    @echo ' '

None.gif

None.gif%.o: ../%.asm

None.gif    @echo 'Building file: $

None.gif    @echo 'Invoking: GCC Assembler'

None.gif    nasm -f elf -d ELF_TYPE -o"$@""$    @echo 'Finished building: $

None.gif    @echo ' '

None.gif

None.gif%.o: ../%.c

None.gif    @echo 'Building file: $

None.gif    @echo 'Invoking: GCC C Compiler'

None.gif    gcc -I"/home/gongjan/jdk1.5.0_08/include/linux"-I"/home/gongjan/jdk1.5.0_08/include/"-O3 -Wall -c -fmessage-length=0-Wl,--add-stdcall-alias -MMD -MP -MF"$(@:%.o=%.d)"-MT"$(@:%.o=%.d)"-o"$@""$    @echo 'Finished building: $

None.gif    @echo ' '

None.gif

None.gif

None.gif


版权声明:本文为weixin_29314405原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。