Java中调用dll动态库的简洁方法
来源:互联网 发布:资生堂红腰子精华 知乎 编辑:程序博客网 时间:2024/05/16 09:42
解压JNative-1.3.2.zip 获得三个文件,分别是:JNativeCpp.dll,libJNativeCpp.so,JNative.jar 。JNativeCpp.dll Windows下用的,拷到windows / system32目录下;
libJNativeCpp.so Linux下使用的;
JNative.jar 这是一个扩展包,加载到你的程序中就可以。
一个简单例子
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;
public class JNativeTest {
public static final int messageBox(int parentHandle, String message,
String caption, int buttons){// throws NativeException, IllegalAccessException {
JNative n = null;
try {
n = new JNative("User32.dll", "MessageBoxA"); //“.dll”不用也可以, 常量DLL_NAME的值为User32.dll
// 构造JNative时完成装载User32.dll,并且定位MessageBoxA方法
n.setRetVal(Type.INT); // 指定返回参数的类型
int i = 0;
n.setParameter(i++, Type.INT, "" + parentHandle);
n.setParameter(i++, Type.STRING, message);
n.setParameter(i++, Type.STRING, caption);
n.setParameter(i++, Type.INT, "" + buttons); // 指定位置上的参数类型和值
n.invoke(); // 调用方法
return Integer.parseInt(n.getRetVal());
}
catch(Exception ex){
ex.printStackTrace();
}
finally {
if (n != null)
try {
n.dispose();
} catch (NativeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 记得释放
}
return 0;
}
public static void main(String[] args) throws NativeException, IllegalAccessException{
JNativeTest.messageBox(100,"这是使用jnative练习", "jnativetest", 1);
}
}
关于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 '' ''
- java中调用 dll 动态库的简洁方法 JNative
- Java中调用dll动态库的简洁方法
- Java中调用动态库dll的方法
- 使用Java调用DLL动态链接库(如何在Java中调用DLL方法)
- Delphi中动态调用DLL的方法
- Delphi中动态调用DLL的方法
- Java下(Eclipse中)调用DLL动态链接库的方法
- java调用dll动态库方法
- java调用dll动态库文件方法
- 动态链接库(dll)的调用方法
- java中调用dll的方法
- Java调用.dll库文件中定义的方法
- 如何动态调用DLL中类的方法以及属性
- 如何动态调用DLL中类的方法以及属性
- 如何动态调用DLL中类的方法以及属性
- 如何动态调用DLL中类的方法以及属性
- 如何动态调用DLL中类的方法以及属性
- java 调用 dll动态库
- 帽子问题
- Android -- Camera 照相和录像, 使用系统APP和自己生成Camera
- Makefile语法简介
- linux下安装vsftp(自己验证)
- 断开某tcp连接
- Java中调用dll动态库的简洁方法
- 【SAP】笔试题
- hdu 1024 Max Sum Plus Plus
- 有关ICP(Internet Cache Protocol)协议的总结(三)
- javascript中的闭包使用例子与解释
- 23种设计模式(5):原型模式
- Ext2文件系统布局,文件数据块寻址,VFS虚拟文件系统
- 编程珠玑:变位词程序的实现
- Windows环境下搭建基于Eclipse的STM32 GCC 开发环境(四) RAM调试 和 FLASH调试设置