-
java使用JNA(Java Native Access)调用dll的方法
2020-09-04 22:03:35java使用JNA(Java Native Access)调用windows系统的dll文件的例子 -
java native 调dll
2011-06-17 18:02:30package ... public class CreateCetByCmd { static{ ... System.loadLibrary("... //注意:加载动态链接库,不写扩展名,名字要与dll的文件名一样 ... public native void creat...package com.hintsoft.pubwin.netcafe.customerservice; public class CreateCetByCmd { static{ System.loadLibrary("certdll"); //注意:加载动态链接库,不写扩展名,名字要与dll的文件名一样 } public native void createCertByCmd(String sCmd, String sParam); public static void main(String[] args){ CreateCetByCmd test = new CreateCetByCmd(); test.createCertByCmd("notepad",""); } }
第一:编写 CreateCetByCmd.java源代码
第二:javac CreateCetByCmd.java
第三:javah CreateCetByCmd 生成CreateCetByCmd.h头文件
第四:编写CreateCetByCmd.c
第五:生成动态链接库 .dll
第六:javac CreateCetByCmd.java
第七:java CreateCetByCmd
注意:
1、建的dll工程里一定要是.c文件。
2、一定要是release编译的dll文件,不要用debug生成的。
3、编绎选项问题,编绎选项挂了本地依赖(此种方法可能在本机上测试是成功的,但是在别人机器上就不行,切忌)
dll存放的位置:
dll放到环境变量的path的任何bin目录下都是可以的。
若在本地文件夹测试dll程序是否通过,注意dll必须要与文件名方的位置一致。
例如:
java类的包名是package com.hintsoft.pubwin.netcafe.customerservice
在D:\cert\com\hintsoft\pubwin\netcafe\customerservice里面放你的java代码
dll放在com的同级目录
在cmd里面执行
d:
cd D:\cert
javac com\hintsoft\pubwin\netcafe\customerservice\test.java
java com.hintsoft.pubwin.netcafe.customerservice.test
如果dll没问题,这时就成功了。若报错则参照我的注意事项。
-
windows下生成jni的dll文件(java native方法)
2017-05-14 06:44:06编写java类, 类中有一个native方法。public class Hello { public native void sayHello(); static { System.loadLibrary("HelloImpl");//加载生成的dll文件 } public static void main(String[] args) {编写java类, 类中有一个native方法。
public class Hello { public native void sayHello(); static { System.loadLibrary("HelloImpl");//加载生成的dll文件 } public static void main(String[] args) { new Hello().sayHello(); } }
生成.class文件
执行命令: javac Hello.java
生成.h文件
执行命令: javah Hello
.h文件的内容:
编写方法的实现 HelloImpl.c
下载安装mingw_64位, 并将其安装目录配置到系统环境变量
mingw_64位最新下载地址:mingw_64位最新版下载地址
该软件的作用是在windows系统使用gcc和g++命令,对c文件和c++文件进行编译,生成.o文件, 进而生成.dll文件。64位的只能生成64位的dll文件。若要生成32位的dll文件,请下载32位的mingw.
mingw_32最新版下载地址:mingw_32位最新版下载地址
安装完后将{minw安装目录}\mingw64\bin放到path即可。生成.o文件
执行命令: gcc -c -I"%JAVA_HOME%\include" - I"%JAVA_HOME%\include\win32" HelloImpl.c
执行完上述命令会生成HelloImpl.o文件。
生成dll文件
执行命令: gcc -Wl,--add-stdcall-alias -shared -o HelloImpl.dll HelloImpl.o
执行java命令, 验证是否成功。
执行命令: java Hello
或
执行命令: java -Djava.library.path=. Hello
如果没有dll动态链接库,输出结果是:
若存在动态连接库,输出结果是:
The End.
-
Java Native Interface(Java 调用Delphi、C、C++编写的DLL)
2011-01-11 18:05:00Java Native Interface(Java 调用Delphi、C、C++编写的DLL)前些日子,就在两天前,由于公司业务上的需求,需要用Java调用另外一Delphi公司编写的代码,然后再用WebService做相对应的数据交互。当时,彼公司提供了一套测试代码,当然是delphi的代码和一套DLL, 另附DOC接口文档一套。由于公司内部没有Delphi的编程经验,只能写相关的java代码来调用彼公司提供的DLL。
而Java调用DLL有两种方法, 一种就是JNI,Java Native Interface,另外一种就是JNA,Java Native Access。这两种调用方法前一个是JAVA内置支持的,但对DLL的编写有一定的约束,也就是说如果你先写java代码,然后用相关的约束再编写DLL是可行的。另一种情况就是我公司碰到的情况,两个毫无业务来往的公司写的程序,突然之间需要产生调用关系,也就是说DLL的编写是没有任何约束的,这样使用JNI就显得相当困难,除非两个公司能够很好的合作,否则调用将异常困难。当然解决方法就是采用JNA,但JAVA本身并不支持JNA,需要第三方类库的支持,也就是JNA.JAR,这个类库可以在java.net上找到该开源工程的主页,里面有对应的文档和示例代码。
但不管是JNI还是JNA,其与DLL交互的原理都是通过计算机系统的底层来实现的。拿windows来说,java与dll的交互是通过windows的com组件来交互的,也就是说com是中间的接口。那么java调用DLL的最基本的约束就出来了:
java调用DLL时,如果涉及到参数传递时,那么参数的类型必须在com中有相关的对应关系,否则不可能产生调用关系(Type Mapping可以参考http://www.dynamsoft.com/help/TWAIN/Guide_DataMapping.htm)。
拿DLL中的结构体来说,C、C++中都是用struct来声明,Delphi中是用record来声明,如果你调用一个带有结构体的参数的方法,那么在DLL的源码中,如果你直接将结构体做为参数传入涵数里面,那么这个结构体在COM中是没有对应的数据类型的,调用将不可能成功。你所需要做的是将这个结构体的引用或指针做为参数传入,这时这个参数也就是一个地址,一个十六进制对应的数字,这样一来调用就不会存在问题。
例如,拿Delphi的代码来说:
这时copyw这个函数将不可能被JAVA调用,你需要将每一个参数的数据类型改为指针PTman或者引用@Tman,这样才能产生正确的调用。
以上就是这次技术碰撞我所体会到的,写出来供大家分享一下,请多多指教。
-
How to Load a Java Native/Dynamic Library (DLL)
2014-11-12 11:38:46How to Load a Java Native/Dynamic Library (DLL) There are several ways to make it possible for the Java runtime to find and load a dynamic library (DLL) at runtime. I will list them bri(MS Windows)
How to Load a Java Native/Dynamic Library (DLL)There are several ways to make it possible for the Java runtime to find and load a dynamic library (DLL) at runtime. I will list them briefly here, followed by examples and further explanation below.
- Call System.load to load the DLL from an explicitly specified absolute path.
- Copy the DLL to one of the paths already listed in java.library.path
- Modify the PATH environment variable to include the directory where the DLL is located.
- Specify the java.library.path on the command line by using the -D option.
- If using Eclipse, set the java.library.path in Eclipse for development/debugging.
1. Call System.load to load the DLL from an explicitly specified absolute path.
This choice removes all uncertainty, but embeds a hard-coded path within your Java application. Example:
import com.chilkatsoft.CkZip; public class Test { static { try { System.load("C:/chilkatJava/chilkat.dll"); } catch (UnsatisfiedLinkError e) { System.err.println("Native code library failed to load.\n" + e); System.exit(1); } } public static void main(String argv[]) { CkZip zip = new CkZip(); System.out.println(zip.version()); } }
2. Copy the DLL to one of the paths already listed in java.library.path
To see the current value of the PATH environment variable, open a MS-DOS prompt and type:
echo %PATH%
Another way of viewing the java.library.path is to run this Java code:
String property = System.getProperty("java.library.path"); StringTokenizer parser = new StringTokenizer(property, ";"); while (parser.hasMoreTokens()) { System.err.println(parser.nextToken()); }
Note: The java.library.path is initialized from the PATH environment variable. The directories may be listed in a different order, and the current directory "." should be present in java.library.path, but may not be listed in the PATH environment variable.
The loadLibrary method may be used when the directory containing the DLL is in java.library.path. To load "chilkat.dll", call System.loadLibrary("chilkat"), as shown here:
import com.chilkatsoft.CkZip; public class Test { static { try { System.loadLibrary("chilkat"); } catch (UnsatisfiedLinkError e) { System.err.println("Native code library failed to load.\n" + e); System.exit(1); } } public static void main(String argv[]) { CkZip zip = new CkZip(); System.out.println(zip.version()); } }
3. Modify the PATH environment variable to include the directory where the DLL is located.
Do this by modifying the PATH environment variable from the Windows Control Panel.
Set PATH on Windows XP:
- Start -> Control Panel -> System -> Advanced
- Click on Environment Variables, under System Variables, find PATH, and click on it.
- In the Edit windows, modify PATH by adding the location of the class to the value for PATH. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the class as the value.
- Close the window.
- Reopen Command prompt window, and run your java code.
Set Path on Windows Vista:
- Right click “My Computer” icon
- Choose “Properties” from context menu
- Click “Advanced” tab (“Advanced system settings” link in Vista)
- In the Edit windows, modify PATH by adding the location of the class to the value for PATH. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the class as the value.
- Reopen Command prompt window, and run your java code.
Important: Setting the PATH environment variable from a MS-DOS command prompt has no effect on java.library.path. For example, this does not work:
set PATH=c:\chilkatJava;%PATH% java Test
Also, modifying the java.library.path from within Java code does not work either:
static { try { // Adding a directory to java.library.path here will not change anything. // System.loadLibrary will still look in the directories listed in java.library.path // as it existed at the very start of the program. // The extra directory path added to java.library.path will not // be searched by loadLibrary. String libpath = System.getProperty("java.library.path"); libpath = libpath + ";C:/chilkatJava"; System.setProperty("java.library.path",libpath); System.loadLibrary("chilkat"); } catch (UnsatisfiedLinkError e) { System.err.println("Native code library failed to load.\n" + e); System.exit(1); } }
4. Specify the java.library.path on the command line by using the -D option.
For example:
java -Djava.library.path=c:\chilkatJava TestApp
5. If using Eclipse, set the java.library.path in Eclipse for development/debugging.
- Open Project->Properties, select “Java Build Path”, click on the “Add External JARs…” button and add the “chilkat.jar”
- (still within the Project Properties dialog) Click on the “Run/Debug Settings”, select your Java class, then click on the “Edit…” button. Select the “Arguments” tab, then add -Djava.library.path=”C:\chilkatJava;${env_var:PATH}” where “C:\chilkatJava” is the directory path containing the “chilkat.dll” file.
from:http://www.chilkatsoft.com/java-loadLibrary-Windows.asp -
Java Native编程
2019-07-11 19:11:14native关键字表示声明的方法不是用Java语言实现,而是用C/C++语言实现的,其被编译成了动态链接库DLL,通过调用DLL来实际完成,即:JNI,java native interface; 2.构建JNI步骤 2.1.建立JNI工具类 package com.test; ... -
Java native关键字的使用方法及Java 调用 dll库的步骤
2018-06-27 11:19:002019独角兽企业重金招聘Python工程师标准>>> ... -
java native关键字
2019-10-07 23:59:16使用native关键字说明这个方法是原生函数,也就是这个方法是用C/C++语言实现的,并且被编译成了DLL,由java去调用。 这些函数的实现体在DLL中,JDK的源代码中并不包含,你应该是看不到的。对于不同的平台它们也是... -
java native方法
2020-01-21 11:06:42这个java类带有一个native方法和一个测试方法把这个类编译把这个类生成头文件(.h)写一个c文件,让这个c文件里的c方法引用.h文件,c文件叫做Meet.c把Meet.c生成Meet.o文件把这个o文件转成dll文件,生成Meet.dll ... -
Java native关键字
2020-10-24 00:53:09native关键字修饰的方法(只能用于修饰方法)表明这些方法是本地化的方法,也即不是在源码中实现,而是在更底层的途径(如c/c++编写的dll文件等等)。 被native关键字修饰的方法不需要在源码中编写实现,而是由... -
java native 关键字
2017-08-09 09:45:35native 这个关键是代表这个方法是原生态的方法,方法对应的实现不是在此文件实现的,而是其他语言实现的,比如c/c++ java 语言本身不能对操作系统底层进行操作和访问,可以通过JNI来调用其他语言。 JNI可以使java... -
java之native及jni生成dll总结
2018-07-15 22:24:46Java不是完美的,Java的不足除了体现在运行速度上要比传统的C++慢许多之外,Java无法直接访问到操作系统底层(如系统硬件等),为此Java使用native方法来扩展Java程序的功能。 -
Java Native 方法和 JNI
2020-07-27 08:17:35JNI 的全称是 Java Native Interface, Java 程序通过调用JNI 调用非java语言编写的方法。JNI支持Java在不同平台的运行,只要操作系统配备本地存储JNI的dll文件,Java程序可以调用JNI。 &... -
JNA(Java Native Access )
2011-07-27 22:43:00JNA(Java Native Access )提供一组Java工具类用于在运行期动态访问系统本地库(native library:如Window的dll)而不需要编写任何Native/JNI代码。开发人员只要在一个java接口中描述目标native library的函数与... -
关于native关键字 java调用DLL
2011-03-09 13:09:18java native方法是指本地方法,当在方法中调用一些不是由java语言写的代码或者在方法中用java语言 直接操纵计算机硬件时要声明为native方法。 java中,通过JNI(Java Native Interface,java本地接口)来实现本地...
-
FFmpeg4.3系列之26:视频监控之H265多路摄像头播控项目实战
-
【Leetcode】1037. Valid Boomerang
-
DedeCms标签生成器(2.6).exe
-
计算机病毒分为哪三类
-
MANIFOLD 2-G 掉坑手册
-
分子生物学二次数据库资源平台的构建.pdf
-
hadoop自动化运维工具Ambari应用实践
-
内部类与同步
-
A算法求解八数码难题.pdf
-
蓝桥杯 入门训练 BEGIN-3 圆的面积
-
PTA教育超市《函数部分》理论题
-
T+专属云15.0新增功能
-
08 并行计算-清华大学薛巍.zip
-
Hcie第四天实验
-
antdesign中手动设置/修改form组件的值
-
前端知识:跨域-演化历史
-
bootstrap remote用法是什么
-
最方便的屏幕颜色拾取器 V1.0.0.exe.zip
-
T+V15.0专属云年结流程及相关问题
-
【数据分析-随到随学】数据分析基础及方法论