Tell the Java VM to Load the DLL
来源:互联网 发布:mac版xampp使用教程 编辑:程序博客网 时间:2024/06/05 10:19
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;Note that the current directory is inserted into the
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
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.)
- Tell the Java VM to Load the DLL
- [Django] Tell your site how to load the template files?
- To tell you the truth
- Failed to load the sqljdbc_auth.dll
- Java连接SQL Server2005 “Failed to load the sqljdbc_auth.dll” 问题解决
- 对于 java 连接sql server 2005的警告: Failed to load the sqljdbc_auth.dll
- eclipse 错误failed to load the jni shared library C:program files/java/ server/jvm.dll
- Failed to load the sqljdbc_auth.dll cause :- no sqljdbc_auth in java.library.path
- failed to load the jni shared library "E:/java/jdk../jvm.dll"解决办法
- Android Failed to load the JNI shared library jvm.dll
- Failed to load the JNI shared library jvm.dll
- Failed to load the JNI shared library jvm.dll
- Failed to load the JNI shared library jvm.dll
- Failed to load the JNI shared library jvm.dll
- JDBC 连接SQLServer数据库(Failed to load the sqljdbc_auth.dll
- Failed to load the JNI shared library jvm.dll
- Failed to load the JNI shared library jvm.dll
- failed to load the jni shared library client jvm.dll
- Scott Mitchell 的ASP.NET 2.0数据教程之二:创建一个业务逻辑层
- 在Asp.net 2.0中操作数据::母板页和站点导航
- 什么是CDN
- 实现编号查询的另类思路
- 什么是DHCP?
- Tell the Java VM to Load the DLL
- 强制性qq聊天
- 报道呗
- 最大正向匹配算法
- 格式化数字函数formatnumber技巧
- ConfigurationSettings类解析
- 建行业务中的一个BUG
- urlrewrite 学习
- 中文分词