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.

(0)

相关推荐

  • Win10下Windows Media Player无法加载字幕的解决方法

      Win10下Windows Media Player无法加载字幕的解决方法: 1.首先安装第三方解码包,我使用的是ADVANCED Codecs(windows 10 codecs); 2.然后删 ...

  • XP系统下手动加载Q77芯片组的AHCI驱动安装教程

    故障现象: 主机Q77芯片组主板,已经在IDE硬盘模式下安装了XP系统,希望指导在Windows XP系统下手动加载AHCI驱动 解决方案: 注意: 1. 确认用户当前BIOS中硬盘模式为IDE,如果 ...

  • Edge浏览器如何在Internet Explorer模式下重新加载网页

    Edge是Windows系统自带的浏览器软件,有些新用户不知道该软件如何在Internet Explorer模式下重新加载网页,接下来小编就给大家介绍一些具体的操作步骤.具体如下:1. 首先第一步先打 ...

  • 无法加载 DLL“xxx.dll”: 找不到指定的模块

    使用vs编程的时候报错 无法加载 DLL"xxx.dll": 找不到指定的模块,这里我是用的是vc++.net,不是c#,所以调用方法有所不同 操作方法 01 首先查找报错的模块对 ...

  • Windows环境下PR2/PR2E的使用介绍

    PR2/PR2E可在Windows环境下利用OLIVETTI、IBM、OKI等仿真选择的打印驱动程序完成各种软件的打印功能,从以上几种仿真的使用效果来看,我们建议用户最好使用IBM Proprinte ...

  • Windows环境下安装Redis体验谈

    Redis 是一个高性能的key-value数据库, 使用内存作为主存储,数据访问速度非常快,当然它也提供了两种机制支持数据持久化存储.比较遗憾的是,Redis项目不直接支持Windows,Windo ...

  • Windows环境下PR2/PR2E如何使用

    PR2/PR2E可在Windows环境下利用OLIVETTI、IBM、OKI等仿真选择的打印驱动程序完成各种软件的打印功能,从以上几种仿真的使用效果来看,我们建议用户最好使用IBM Proprinte ...

  • windows环境下如何删除Mysql

    windows环境下如何删除Mysql 第一步:关闭mysql服务 我的电脑 -> 管理 点击管理出现如下界面 服务和应用程序 ------------>双击服务 出现如下图: 然后找到m ...

  • 加载dll时提示调用失败怎么办?

    有时候加载某个DLL时,提示对DllRegisterServer调用失败,这可能是由于权限不足,因此用管理员权限进行操作,详细步骤如下. 操作方法 01 单击打开开始菜单,然后点击所有程序. 02 找 ...