winder

          DB&JAVA
          posts - 2, comments - 6, trackbacks - 0, articles - 11
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          java中調用 dll 動態庫的簡潔方法 JNative

          Posted on 2008-05-19 17:45 winderain 閱讀(1072) 評論(0)  編輯  收藏 所屬分類: JAVA
          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 ' '


           

          主站蜘蛛池模板: 县级市| 建平县| 林甸县| 夏河县| 林芝县| 新化县| 秦皇岛市| 冀州市| 北海市| 新巴尔虎左旗| 剑阁县| 定州市| 嘉兴市| 揭阳市| 垣曲县| 大足县| 湘潭市| 安乡县| 鲁山县| 连云港市| 嫩江县| 房产| 宜君县| 昆明市| 灵璧县| 三都| 波密县| 惠安县| 九寨沟县| 廉江市| 涿州市| 乳山市| 武清区| 崇阳县| 英吉沙县| 光泽县| 衡南县| 会泽县| 雅江县| 都江堰市| 高雄县|