欢迎登陆真网站,您的到来是我们的荣幸。 登陆 注册 忘记密码? ☆设为首页 △加入收藏
欢迎加入真幸福QQ群
电脑知识: 基础知识 网络技术 操作系统 办公软件 电脑维修 电脑安全 windows7 windows8 windows10 服务器教程 平板电脑 视频播放教程 网络应用 互联网 工具软件 浏览器教程 QQ技巧 输入法教程 影视制作 YY教程 wps教程 word教程 Excel教程 PowerPoint
云南西双版纳特产小花糯玉米真空包装


Windows下统计行数的命令
windows下安装zookeeper
linux进程创建
linux中exit()和_exit区别
360云盘忘记帐号密码怎么办
Linux文件:ls、cp、mv、rm
openstack常用命令
windows下利用文件映射实现共享内存
linux中mysql开机自动启动3种方法
思科交换机配置命令
Windows环境下Java加载DLL
【 来源:网络 】【 点击:2 】 【 发布时间:2017_03_03 08:59:59 】

   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.

本网站由川南居提供技术支持,fkzxf版权所有 浙ICP备12031891号
淳安分站 淳安分站