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:


C:/>echo %PATH%
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) {

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 "").

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:

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.)


Show Inetusers: 展示当前服务器在线人数 tell amgr run "highcomoa/application/udf_fwgl.nsf" 'agent_sh' :通过命令直接在服...
  • szheng1989
  • szheng1989
  • 2011年11月13日 21:54
  • 4491

OpenProj打开不了或者提示"Failed to load Java VM Library"的错误的解决方案

OpenProj打开不了或者提示"Failed to load Java VM Library"的错误的解决方案
  • coolham
  • coolham
  • 2015年11月20日 00:01
  • 2763


  • lenf
  • lenf
  • 2009年02月04日 23:06
  • 7918

ASP.NET和Oracle连接问题的解决方法——Unable to load DLL (oci.dll)

不少人在做ASP.NET+Oracle开发的时候都会碰到连接问题,提示“Unable to load DLL (oci.dll)”,但这个文件在系统中是存在的,很多人为此焦头烂额,我看到很多的帖子都是...
  • heyixiang
  • heyixiang
  • 2005年08月09日 19:02
  • 5712

System.DllNotFoundException Unable to load DLL SQLite.Interop.dll解决办法

最近在写一个c#的sdk,用到了sqlite for .net! 写完之后,打包给别人调用,结果在两台客户机上面死活不可用。然后开始查原因,最后定位到问题: System.DllNotFoundE...
  • songtzu
  • songtzu
  • 2015年10月26日 20:48
  • 2203

Cannot load resource dll:REPLRES.RLL解决

进入注册表,把REPLRES.RLL文件都删了:做法 在开始的搜索程序与文件中打进:regedit在注册表中点击编辑,选择查找下一个,输入REPLRES.RLL把找到的REPLRES.RLL文件都删...
  • haihuanjack
  • haihuanjack
  • 2012年08月14日 09:43
  • 7981

[Navicat] 连接Oracle报错_Cannot load OCI DLL 87

Navicat可以用来连接各种主流数据库,真是个不错的DB工具。 如果你第一次用Navicat尝试连接oracle数据库,很可能报下面的错: Cannot load OCI DLL 87: ...
  • szx1999
  • szx1999
  • 2015年12月08日 14:54
  • 36069

Openproj 在64位操作系统报错errno=193

一、双击打开OpenProj.exe没反应的解决方案: 1) 修改OpenProj1.4.0.ini,将Maximum Version=any改为Maximum Version=1.7,保存。 ...
  • cool_126
  • cool_126
  • 2016年12月12日 11:57
  • 1406

Unable to load DLL 'OraOps10.dll

起因:本地32位系统,oracle为10g,要部署到64系统oracle 11g的服务器上,后台与前台都要部署上去。 经过:一开始直接在本地调试后台程序,然后直接复制到了服务器上,结果报错: Una...
  • cs6480012
  • cs6480012
  • 2015年12月18日 16:44
  • 818

JNI开发Can't load IA 32-bit .dll on a AMD 64-bit platform错误的解决

在JNI开发中,Java程序需要调用操作系统动态链接库时,报错信息:Can't load IA 32-bit .dll on a AMD 64-bit platform。这是由于你的版本是Window...
  • cbbbc
  • cbbbc
  • 2015年05月16日 22:28
  • 2894
您举报文章:Tell the Java VM to Load the DLL