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.

“张承辉博客” Windows环境下Java加载DLL https://www.zhangchenghui.com/126567

(0)
上一篇 2022年5月9日 下午10:22
下一篇 2022年5月9日 下午10:22

相关推荐

  • 记丐侠 文言文翻译

    记丐侠 文言文翻译 来懋斋先生,家庭极为贫穷,但是为人性情慷慨有过人的节操。参加乡试后,考中举人。打算赴京参加礼部的进士考试,然而没有资金路费。当地乡里有一种习俗,按照旧例凡是有人…

    问答 2022年4月18日
    002
  • 真正笑破肚皮的笑话

    1、如果你觉得自己又穷又丑,请不要悲伤,你还有希望,至少你的判断是正确的。 2、那些过不去的坎,都是因为你腿短!好看的皮囊嫌你丑,有趣的灵魂厌你俗。 3、女孩子晚上千万不要一个人出…

    问答 2022年4月22日
    004
  • 端午思念情人句子

    无奈,再翻开你过去的情书,不禁泪如雨下!心里不禁升起一阵怨恨,竟把它们烧成了纸灰,又把眼泪咽了下去。如果有一天纸的灰烬能重新燃起,请让它燃烧我那充满爱之火的温暖的心!以下是张承辉为…

    问答 2022年4月17日
    0042
  • 2022年河南有哪些体育类大学

    想要报考体育类大学的考生,你们知道各省份都有哪些体育类院校吗?小编整理了河南的体育类院校名单,欢迎大家查阅了解。 2022年河南有哪些体育类大学河南比较好的院校有嵩山少林武术职业学…

    问答 2022年5月10日
    002
  • 工地上的沙子能种多肉

    一、工地上的沙子能种多肉吗 工地上的沙子能用来种多肉。沙子的透气性比较好,不容易导致土壤积水,能提高土壤的透气性。注意,种植多肉植物的时候不可用纯沙子,否则保水性差,且不含养分,对…

    问答 2022年6月10日
    004
  • 致高考阅卷老师的公开信

    致高考阅卷老师的公开信 尊敬的阅卷老师: 您好! 感谢您在炎炎夏日还不辞劳苦地为我们阅卷!我们向您致以崇高的敬意! 作为刚刚走出考场的2010届高中毕业生,高考对于我们的重要性不言…

    问答 2022年4月18日
    005

发表评论

登录后才能评论