Windows环境下Java加载DLL

   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.

时间: 2024-09-19 04:59:30

Windows环境下Java加载DLL的相关文章

解决Windows 10下无法加载桌面背景的问题

1.重启电脑了,我们可以尝试重启电脑看看可以解决不,或者是设置桌面壁纸试一下了,具体:右击桌面空白处,然后点击"个性化"选项,进入后就可以设置壁纸了. 2.使用WIN+R组合键调出运行,输入regedit,调出注册表编辑器(注册表修改前最好是备份了,这个在注册表编辑器上面的菜单可以直接导出备份了): 2.定位到\HEKY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\WINDOWS NT\CURRENTVERSION\WINLOGON,在右侧找到shell,双击打开

java 加载dll介绍(转)

最近在做的工作要用到本地方法,需要在Java中加载不少动态链接库(以下为方便延用Windows平台下的简写dll,但并不局限于Windows).刚刚把程序跑通,赶紧把一些心得写出来,mark.也希望对大家的类似工作有所帮助  首先,应当明确,dll有两类:(1)Java所依赖的dll和,(2)dll所依赖的dll.正是由于第(2)种dll的存在,才导致了java中加载dll的复杂性大大增加,许多说法都是这样的,但我实验的结果却表明似乎没有那么复杂,后面会予以详细阐述.  其次,Java中加载dl

SQLite部署-无法加载 DLL“SQLite.Interop.dll”: 找不到指定的模块

近期刚使用SQLite,主要引用的是System.Data.SQLite.dll这个dll,在部署到测试环境时报无法加载 DLL"SQLite.Interop.dll": 找不到指定的模块. (异常来自 HRESULT:0x8007007E). 后上官网查了下,sqlite-netFx40-binary-Win32-2010-1.0.94.0.zip 这个包中的System.Data.SQLite.dll是需用到SQLite.Interop.dll,但是这个dll不是.net版本的无法

windows编程,加载dll库示例

第一种方式,手动找到函数地址,进行调用. LoadLibrary(): GetProcAddress(): 生成dll库 #include<Windows.h> //导出函数,可以加载的时候调用 _declspec(dllexport) void msg() { MessageBoxA(0, "1", "2", 0); } //导出函数,可以加载的时候调用 _declspec(dllexport) int add(int a, int b) { retu

64位Unity3D编辑器下 无法加载sqlite3.dll

问题描述 64位Unity3D编辑器下 无法加载sqlite3.dll Failed to load 'Assets/Plugins/sqlite3.dll' expected 64 bit architecture (IMAGE_FILE_MACHINE_AMD64) but was IMAGE_FILE_MACHINE_I386.Mono.Data.Sqlite.SQLite3:Open(String SQLiteOpenFlagsEnum Int32 Boolean)Mono.Data.S

java jni加载dll文件异常

问题描述 java jni加载dll文件异常 Can't load this .dll (machine code=0x101) on a AMD 64-bit platform 解决方案 32位,64位兼容性的问题吧,, 解决方案二: 可能是32位和64位的兼容问题

Vista下使用VS2008编译.EXE的C#程序 出现错误&amp;amp;quot;无法加载 DLL“api_clnt.dll”: 应用程序无法启动,因为应用程序的并行配置不正确。有关详细信息,请参阅应用程序事件日志。 (异常来自 HRESULT:0x800736B1)&amp;amp;quot;。

问题描述 Vista下使用VS2008编译.EXE的C#程序出现错误"无法加载DLL"api_clnt.dll":应用程序无法启动,因为应用程序的并行配置不正确.有关详细信息,请参阅应用程序事件日志.(异常来自HRESULT:0x800736B1)".api_clnt.dll是由C++语言实现的我想问下这个错误是由VISTA系统引起的,还是VS2008引起的呢,因为之前在XP系统下用VS2005编译时是正确的还有怎么解决谢谢 解决方案 解决方案二:先给自己顶下解决方

利用xwizard.exe加载dll

本文讲的是利用xwizard.exe加载dll,在之前的文章介绍了利用Excel.Application object's RegisterXLL()加载dll的技巧.本文继续,介绍最近学习到的一种更为通用的方法--利用xwizard.exe加载dll.  该方法最大的特点是xwizard.exe自带微软签名,在某种程度上说,能够绕过应用程序白名单的拦截. 0x01 简介 本文将要介绍以下内容: · xwizard.exe简介 · 利用思路 · 实际测试 0x02 xwizard.exe简介 应

ZF2入门:Windows环境下从零开始Zend Framework 2.0 (ZF2)环境搭建

原文:http://avnpc.com/pages/zend-framework-2-installation-for-windows 日志未经声明,均为AlloVince原创.版权采用『 知识共享署名-非商业性使用 2.5 许可协议』进行许可. Zend Framework 2.0 (ZF2)正式发布之后不少朋友都进行了尝试,可能由于ZF2涉及到的新特性比较多,有朋友希望能有一篇从零开始Zend Framework 2.0 (ZF2)的教程,于是就有了本篇日志. 以下将记录在Windows环境