20xx年04月22日 星期四 上午 10:57
开源组件Jnative使用
? 下截JNative组件
/ 到这里下载JNative开源项目,我下载的是1.3.2
? 解压JNative-<st1:chsdate isrocdate="False" islunardate="False" day="30" month="12" year="1899">1.3.2</st1:chsdate>.zip
获得三个文件,分别是:JNativeCpp.dll,libJNativeCpp.so,JNative.jar 。
JNativeCpp.dll Windows下用的,拷贝到windows / system32目录下;
libJNativeCpp.so Linux下的,拷贝到系统目录下;
JNative.jar 这是一个扩展包,导入工程LIB中或将其拷贝到jdk\jre\lib\ext 下,系统会自动加载。 ? 使用说明
我的项目将使用JNative组件调用一个测试应用服务器状态的TestAppSvr.dll文件,Dll文件中包含一个TestConnect()方法,返回一个整形的结果(1或0)
首先配置好JNative组件的windows环境:
将Native要用到JNativeCpp.dll放在系统盘的\WINDOWS\system32下
将JNative.jar导入工程中,新建一个调用类:
java 代码
1. package com.tvjody;
2.
3. import java.io.File;
4. import java.io.FileOutputStream;
5. import java.io.IOException;
6. import java.io.InputStream;
7.
8. import org.xvolks.jnative.JNative;
9. import org.xvolks.jnative.Type;
10. import org.xvolks.jnative.exceptions.NativeException;
11.
12. public class AppSvrTestConnect {
13.
14. public AppSvrTestConnect() {
15.
16. }
17.
18. /**
19. * 测试应用服务器连接状态
20. *
21. * TestConnect
22. * @param ip 应用服务器IP
23. * @param port 端口
24. * @param intrcpt 是否采用数据压缩方式 1 :true 0:false
25. * @return int 1 :成功 0:失败
26. * @throws NativeException
27. * @throws IllegalAccessException
28. */
29. private static final int TestConnect(String ip, int port, int intrcpt)throws NativeException, IllegalAccessException {
30. JNative n = null;
31. try {
32. n = new JNative("TestAppSvr.dll", "TestConnect");
33. n.setRetVal(Type.INT);
34. int i = 0;
35. n.setParameter(i++, Type.STRING, ip);
36. n.setParameter(i++, Type.INT, "" + port);
37. n.setParameter(i++, Type.INT, "" + intrcpt);
38. n.invoke();
39. return Integer.parseInt(n.getRetVal());
40. } finally {
41. if (n != null)
42. n.dispose();
43. }
44. }
45. /**
46. * 指定Dll文件路径,动态加载本地链接库,测试应用服务器连接状态
47. * setDllPath
48. * @param path Dll文件的路径,不包含DLL名称 例如:windows - d:\test\test\ unix - root/test/test/
49. * @param ip 应用服务器IP
50. * @param port 端口
51. * @param intrcpt 是否采用数据压缩方式 1 :true 0:false
52. * @return int 1 :成功 0:失败
53. * @throws NativeException
54. * @throws IllegalAccessException
55. */
56. public static final int TestConnectFromDllPath(String path,String ip, int port, int intrcpt) throws NativeException, IllegalAccessException{
57. path += "TestAppSvr.dll";
58. System.load(path);
59. return TestConnect(ip,port,intrcpt);
60. }
61. /**
62. * Dll文件放在JRE\bin目录下面,ClassLoader就能通过System.loadLibrary()动态加载本地链接库
63. * TestConnectFromDllPath
64. * @param ip 应用服务器IP
65. * @param port 端口
66. * @param intrcpt 是否采用数据压缩方式 1 :true 0:false
67. * @return int 1 :成功 0:失败
68. * @throws NativeException
69. * @throws IllegalAccessException
70. */
71. public static final int TestConnectFromDllPath(String ip, int port, int intrcpt) throws NativeException, IllegalAccessException{
72. System.loadLibrary("TestAppSvr");
73. return TestConnect(ip,port,intrcpt);
74. }
75. }
这个类实现了一个静态私有方法,用来调用Dll文件中的方法返回结果
private static final int TestConnect(String ip, int port, int intrcpt)
两个静态公共方法,分两种方式装载DLL文件
public static final int TestConnectFromDllPath(String path,String ip, int port, int intrcpt) //通过DLL文件的路径
public static final int TestConnectFromDllPath(String ip, int port, int intrcpt) //通过ClassLoader
然后新建一个类,调用AppSvrTestConnect.java,实现方法一调用,我是将TestAppSvr.dll文件与Demo.java放在一个目录下 ,所以得到Demo.java的路径后就可以得到TestAppSvr.dll的路径,调用AppSvrTestConnect.TestConnectFromDllPath()方法后就能返回正确的信息.方法二是已经将TestAppSvr.dll放在了Jre\bin目录下,在JVM的Classloader的时候会自动加载,然后通过
System.loadLibrary("TestAppSvr")就可以装配DLL文件.
java 代码
1. public class Demo {
2. public int getInfo() throws NativeException, IllegalAccessException{
3.
4. String path=getClass().getResource(File.separator).getPath();
5. path = path.substring(1,path.length());
6. System.out.println(path); //得到DLL文件的路径
7.
8. String ip = "192.168.0.48"; //服务器IP
9. int port = 221; //端口
10. int intrcpt = 1; //数据压缩方式传送,1为采用;0为不采用
11. //方法1 传入Dll文件的路径
12. //int info = AppSvrTestConnect.TestConnectFromDllPath(path, ip, port, intrcpt); 13.
14. //方法2 Dll文件已经放在JRE\bin目录下面
15. int info = AppSvrTestConnect.TestConnectFromDllPath(ip, port, intrcpt); 16.
17. //1为成功,0为失败
18. if (info == 1)
19. System.out.println("应用服务器可用。");
20. else
21. System.out.println("应用服务器不可用,请检查IP地址和端口是否正确。"); 22.
23. return info;
24. }
25.
System.loadLibrary():装载Windows\System32下或jre\bin或Tomcat\bin目录下的本地链接库 System.load():根据具体的目录来加截本地链接库,必须是绝对路径
? 备注
上面的示例工程,因为是例子,所以没有大多的设计,只是实现了装载DLL文件,调用DLL文件方法,返回信息.
JNative的详细说明,请参考JNative的源程序和例子.
注意JVM只允许一个默认的ClassLoader来load native library,同时并不提供专门的API来unload一个loaded native library,所以在项目调试的时候,独立启动Web Server.
第二篇:如何在Java中调用dll动态库
如何在Java中调用dll动态库
解压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视频 /