Tell the Java VM to Load the DLL

原创 2007年10月13日 08:51:00
 

Tell the Java VM to Load the DLL

There are two different ways to load a native library into a running Java program: System.loadLibrary(String) and System.load(String). The System.loadLibrary method allows us to load a library from the "default" path. System.load allows us to load a library from anywhere via its absolute path.

First, System.loadLibrary. We'll use System.loadLibrary for our example because most other examples use it, and because we're doing all of our work in one directory.

When using System.loadLibrary, the only thing we specify is the name of the DLL file we want. The JVM will search for it in the "Java library path." This is a path which is given by the java.library.path system property (and hence can be altered on the java.exe command line using the -D option). The default value of this appears to be related to the Windows path, though it appears to be somewhat scrambled, and I'm not quite sure how or why. In other words, I'm not sure how the Windows JVM creates the initial value of java.library.path. This is the default on my system:

java.library.path=C:/WINNT/system32;.;C:/WINNT/System32;C:/WINNT;c:/applications/asc/pervasive/BIN;c:/cygwin/bin;
C:/WINNT/system32;C:/WINNT;C:/WINNT/System32/Wbem;/win32

C:/>echo %PATH%
c:/applications/asc/pervasive/BIN;c:/cygwin/bin;C:/WINNT/system32;C:/WINNT;C:/WINNT/System32/Wbem;/win32
Note that the current directory is inserted into the PATH; I believe that this is something that Windows does by default. I am going to execute the program from the directory in which HelloWorld.dll was created, so I won't have to mess with java.library.path. One could also use command-line options to alter java.library.path or simply copy the DLL into one of the Windows directories.

In order to cause the JVM to load the library, we need to alter our Java code:


package example.jni;

public class HelloWorld {
private static native void writeHelloWorldToStdout();

public static void main(String[] args) {
System.loadLibrary("HelloWorld");
writeHelloWorldToStdout();
}
}

Go ahead and re-compile the Java program.

The JVM takes care of resolving "HelloWorld" to "HelloWorld.dll" (on UNIX, it would resolve it to "libHelloWorld.so").

Anyway, we're finished and can proceed to the next step.

If we were to use the System.load method, we'd just insert code like:

System.load("c:/path/to/dll/HelloWorld.dll");
By the way, that's a good non-JNI tip -- the JVM accepts forward slashes just fine, even for Windows file names. Some of my programs (e.g., web programs which involve forward slashes in URLs) have been vastly simplified knowing that, not to mention the lack of annoying escape characters. (Disclaimer: As far as I know, this behavior isn't part of the Java specification, so it's possible it could be changed or operate differently in other VMs.)

安卓模拟器avd无法读取Snapshot的解决方案

首先,为了加快安卓模拟器avd的快速启动,你在配置选项中勾选了Snapshot。(可能有童鞋说:废话!没勾选能出现这样的状况吗?)...
  • xiaomengtianya
  • xiaomengtianya
  • 2013年08月08日 13:10
  • 3454

2014年工作中遇到的20个问题:81-100

81.MySQL执行sql命令的时候,总是报一个陈旧的sql脚本错误。只要一致性,就崩溃,搞的我也要崩溃了。先后尝试了 重新安装MySQL-Front,完全清除MySQL-Front再重新安装,都不行...
  • FansUnion
  • FansUnion
  • 2014年11月12日 17:45
  • 3847

Github使用1-入门

Gighub官网:https://github.com/ Github的入门使用流程: 1、注册github帐号。 2、登陆帐号后创建一个repository。如命名为:GitTest 点击进...
  • Sunnyfans
  • Sunnyfans
  • 2013年12月08日 12:17
  • 10601

java vm 参数及设置(转载)

一、设置JVM内存设置 1. 设置JVM内存的参数有四个: -Xmx   Java Heap最大值,默认值为物理内存的1/4,最佳设值应该视物理内存大小及计算机内其他内存开销而定; ...
  • fantasyagain
  • fantasyagain
  • 2015年01月22日 12:15
  • 4953

java vm args

这个问题主要还是由这个问题 java.lang.OutOfMemoryError: Java heap space 引起的。第一次出现这样的的问题以后,引发了其他的问题。在网上一查可能是JAVA的堆栈...
  • donson_x64
  • donson_x64
  • 2012年06月26日 18:58
  • 6923

Java VM Options You Should Always Use in Production

This post is a cheatsheet with enumeration of options, which should be always used to configure Java...
  • chweiweich
  • chweiweich
  • 2017年02月10日 11:29
  • 215

java vm 帮助文档

  • 2014年01月28日 00:24
  • 165KB
  • 下载

java vm.exe

  • 2011年06月21日 22:03
  • 14.05MB
  • 下载

Java VM 规范

  • 2014年10月23日 18:38
  • 1.1MB
  • 下载

java vm 监控

  • 2014年05月15日 09:54
  • 1.87MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Tell the Java VM to Load the DLL
举报原因:
原因补充:

(最多只允许输入30个字)