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中调用比较简单:

public  String callDll(String cond)      
     
throws  NativeException, IllegalAccessException  {   

    JNative n 
= null;   
    String ret 
= "";
    JNative.setLoggingEnabled(
true); 
    
try {   
        n 
= new JNative(dllName, methodName); // 常量dllName的值为:windows下不加后缀.dll,linux需要包括后缀.so
        n.setRetVal(Type.STRING); // 指定返回参数的类型   
        int i = 0;
        n.setParameter(i
++, Type.STRING, cond);
        n.setParameter(i
++, Type.STRING, cond);
        
        n.invoke(); 
// 调用方法   
        ret = n.getRetVal(); //取回返回值
        
        System.out.println(
"this is call dll:"+ ret+ " ");
    }
 catch(Exception e){
        System.out.println(
"cann't call dll: " + dllName +"." +methodName + " ");
        e.printStackTrace();
           
    }
finally {
        
if (n != null){
            
try{
            n.dispose(); 
// 记得释放
            }
catch(Exception e){
                e.printStackTrace();
                System.out.println(
"can't dispose the dll:" + dllName);
            }

        }

    }
  
    
return ret;
}
  

关于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 文件

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

-include ../makefile.init

RM :
=  rm -rf

# All of the sources participating in the build are defined here
-include sources.mk
-include subdir.mk
-include objects.mk

ifneq ($(MAKECMDGOALS)
, clean)
ifneq ($(strip $(C++_DEPS))
, )
-include $(C++_DEPS)
endif
ifneq ($(strip $(CC_DEPS))
, )
-include $(CC_DEPS)
endif
ifneq ($(strip $(C_DEPS))
, )
-include $(C_DEPS)
endif
ifneq ($(strip $(CPP_DEPS))
, )
-include $(CPP_DEPS)
endif
ifneq ($(strip $(CXX_DEPS))
, )
-include $(CXX_DEPS)
endif
ifneq ($(strip $(C_UPPER_DEPS))
, )
-include $(C_UPPER_DEPS)
endif
endif

-include ../makefile.defs

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

# All Target
all: libJNativeCpp.so

# Tool invocations
libJNativeCpp.so: $(OBJS) $(OBJS_ASM) $(USER_OBJS)
    @echo 'Building target: $@'
    @echo 'Invoking: GCC C++ Linker'
    g++ -shared -o
" libJNativeCpp.so "  $(OBJS) $(OBJS_ASM) $(USER_OBJS) $(LIBS)
    @echo 'Finished building target: $@'
    @echo ' '

# Other Targets
clean:
    -$(RM) $(OBJS)$(C++_DEPS)$(CC_DEPS)$(C_DEPS)$(CPP_DEPS)$(LIBRARIES)$(CXX_DEPS)$(C_UPPER_DEPS) libJNativeCpp.so
    -@echo ' '

.PHONY: all clean dependents
.SECONDARY:

-include ../makefile.targets

 

subdir.mk 文件

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

# Add inputs and outputs from these tool invocations to the build variables 
C_SRCS +
=  
../jni_util.c 
../mem.c 

CPP_SRCS +
=  
../CallBack.cpp 
../WindowProcUtil.cpp 
../org_xvolks_jnative_JNative.cpp 

ASM_SRCS +
=  
../asm_io.asm 
../calls.asm 

OBJS +
=  
./CallBack.o 
./WindowProcUtil.o 
./jni_util.o 
./mem.o 
./org_xvolks_jnative_JNative.o 

OBJS_ASM +
=  
./asm_io.o 
./calls.o

C_DEPS +
=  
./jni_util.d 
./mem.d 

CPP_DEPS +
=  
./CallBack.d 
./WindowProcUtil.d 
./org_xvolks_jnative_JNative.d 


# Each subdirectory must supply rules for building sources it contributes
%.o: ../%.cpp
    @echo 'Building file: $<'
    @echo 'Invoking: GCC C++ Compiler'
    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: $<'
    @echo ' '

%.o: ../%.asm
    @echo 'Building file: $<'
    @echo 'Invoking: GCC Assembler'
    nasm -f elf -d ELF_TYPE -o
" $@ "   " $< "
    @echo 'Finished building: $<'
    @echo ' '

%.o: ../%.c
    @echo 'Building file: $<'
    @echo 'Invoking: GCC C Compiler'
    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: $<'
    @echo ' '


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值