winder

          DB&JAVA
          posts - 2, comments - 6, trackbacks - 0, articles - 11
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
          java中調(diào)用 dll 動(dòng)態(tài)庫(kù)的簡(jiǎn)潔方法 JNative

              Java中調(diào)用DLL方法,是通過JNI接口實(shí)現(xiàn)的,http://www.ibm.com/developerworks/cn/java/l-linux-jni/ 這里有詳細(xì)的說明。大概是先用Java寫一個(gè)接口類,然后用javah 生成一個(gè)xxx.h的c語(yǔ)言的頭文件,然后用C實(shí)現(xiàn)這個(gè)頭文件,在這個(gè)實(shí)現(xiàn)接口中調(diào)用其他已經(jīng)實(shí)現(xiàn)的接口。

              我看到這個(gè)實(shí)現(xiàn)方法后,感覺怪怪的,這不是反過來了么?隨后在網(wǎng)上看到了JNative的說明,原來已經(jīng)有人封裝了這些步驟,使用JNative,你可以直接使用了,而不需要寫什么接口C文件了。JNative下載后有3個(gè)文件,JNative.jar,JNativeCpp.dll以及l(fā)ibJNativeCpp.so,JNativeCpp.dll是用于window系統(tǒng)的;libJNativeCpp.so是用于linux系統(tǒng)的,不過這個(gè)文件是glic2.4下編譯的。如果需要底版本,需要重新編譯。如何編譯,在后面介紹。不管什么系統(tǒng),都需要把DLL文件放到系統(tǒng)路徑下或者是加入到搜索目錄。

            使用說明:

             JAVA中調(diào)用比較簡(jiǎn)單:

          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); // 指定返回參數(shù)的類型   
                  int i = 0;
                  n.setParameter(i
          ++, Type.STRING, cond);
                  n.setParameter(i
          ++, Type.STRING, cond);
                  
                  n.invoke(); 
          // 調(diào)用方法   
                  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;
          }
            

          關(guān)于linux下編譯 C代碼部分說明:

          對(duì)于linux不同版本,可能會(huì)導(dǎo)致libJNativeCpp.so不同
          原帶的libJNativeCpp.so 是在glic2.4下編譯的
          為了適應(yīng)glic2.3的情況,重新編譯了libJNativeCpp.so,在for gcc3.4.6 glibc 2.3下。
          編譯辦法:
             在JNative-1.3.2-src\JNativeCpp\Release目錄下
             1、備份calls.o和 asm_io.o這兩個(gè)Object文件
             2、make clean
             3、恢復(fù)到當(dāng)前目錄calls.o和 asm_io.o這兩個(gè)Object文件
             4、make
            
             目前已經(jīng)修改了Release目錄下的makefile和subdir.mk文件,使得在make clean的時(shí)候不刪除calls.o和 asm_io.o兩個(gè)文件
            
          附:linux 動(dòng)態(tài)庫(kù)搜索路徑:
            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 ' '


           

          主站蜘蛛池模板: 富宁县| 东乌珠穆沁旗| 大余县| 吉林省| 宣化县| 西乡县| 临湘市| 军事| 抚州市| 桐柏县| 益阳市| 尼玛县| 故城县| 扎兰屯市| 抚州市| 孙吴县| 九龙城区| 康定县| 大悟县| 乌拉特后旗| 灌云县| 崇仁县| 嘉义市| 星子县| 古交市| 江陵县| 松桃| 松阳县| 纳雍县| 恭城| 虹口区| 阳山县| 咸阳市| 芮城县| 安溪县| 霍山县| 开化县| 东丰县| 绥中县| 南和县| 满洲里市|