Purpose:
They can be used to load library files, whether JNI library files or non-JNI library files. Before calling any local method, you must first load the corresponding JNI library file with one of the two methods.
In fact, JDK provides two methods for loading files. One is the system. Load (string filename) method, and the other is the system. loadlibrary (string libname) method.
System. LoadThe system. Load parameter must be the absolute path of the library file, which can be any path, for example:
System. load ("C: \ Documents ents and Settings \ testjni. DLL "); // Windowssystem. load ("/usr/lib/testjni. so "); // Linux
System. loadlibrary
The system. loadlibrary parameter is the library file name and does not contain the library file extension.
System. loadlibrary ("testjni"); // load the local testjni. dll library system. loadlibrary ("testjni") in windows; // load the local library libtestjni. So in Linux
Note:
Testjni. dll or libtestjni. So must be in the path pointed to by Java. Library. path in the JVM attribute.
Differences:
1. The loading path is different.
System. Load (string filename) We can see that the jdk api description is "loading code files from the local file system as a dynamic library with the specified file name.The file name parameter must be a complete path name..", Therefore, the parameter of this method must be the complete path of the file to be loaded, with the file suffix.
System. loadlibrary (string libname). We can see that the jdk api description is "loadSystem Library without suffix.The system library refers to Java. Library. path, which is the path mapped to the local system.
Use the system. getproperty (string key) method to view the content pointed to by Java. Library. Path.
123 |
public static void main(String[] args){ System.out.println(System.getProperty( "java.library.path" )); } |
123 |
C:\Program Files\Java\jdk1. 6 .0_45\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32; C:\Windows;C:\Program Files (x86)\Common Files\NetSarang;C:\Program Files\Java\jdk1. 6 .0_45\bin; C:\Program Files\Java\jdk1. 6 .0_45\jre\bin; |
It can be seen that the classpath path is in the window.
2. Notes:Assume that. DLL and B. DLL dependencies, such as. DLL static link B. DLL, if you select system. load ("D:/. DLL "), even if B. DLL is also placed in the D:/path. The load method still cannot find the dependency. DLL file. Because the Java Virtual Machine is loading. DLL, it depends on B. DLL, then go to Java. library. load B under path. DLL, while B. DLL is not in Java. library. path. There are two solutions:
(1) first system. Load ("D:/B. dll") and then system. Load ("D:/a. dll ")
(2) place both a. dll and B. dll under java. Library. path, and then call system. loadlibrary ("")
However, there is a dependency between the two DLL files. A. dll depends on B. dll. I am not very clear about its knowledge in other fields, so I have not tried it myself at the second point. When we put this knowledge point here, we will have a way to solve the problem if we encounter similar problems in the future.
Set the path of Java. Library. Path
1. Windows
(1): Set the classpath path. Generally, the dynamic link library is stored in the directory c:/window/system32.
(2): You can also modify the environment variable to add the address of the dynamic link library.
(3): Set the address of the dynamic library of eclipse during development. For details, see:
Http://blog.csdn.net/ty564457881/article/details/7066423
2. in Linux
You need to set the system variable LD_LIBRARY_PATH to add java. Library. Path.
When JVM is started, the value of the system variable LD_LIBRARY_PATH is used to initialize the java. Library. Path attribute.
Java. Library. Path Default Value
On my machine, the default value is:
$ Java_home/JRE/lib/i386/Server:
$ Java_home/JRE/lib/i386
:
$ Java_home/JRE/../lib/i386:/usr/Java/packages/lib/i386:/lib:/usr/lib
Analysis:
-The first three are the local libraries provided in JRE.
-The last three are the operating system's library file directories.
-Djava. Library. Path and
Difference between LD_LIBRARY_PATH
If you use Java-djava. Library. Path =/JNI/library/path
OverwriteDefault value. The value is:
/JNI/library/path
If you use export LD_LIBRARY_PATH =/JNI/library/path
AppendThe value is in the default value. The value of LD_LIBRARY_PATH will be inserted into the JRE local library before operating the system library file directory. The value is:
$ Java_home/JRE/lib/i386/Server:
$ Java_home/JRE/lib/i386
:
$ Java_home/JRE/../lib/i386:/JNI/library/path:/usr/Java/packages/lib/i386:/lib:/usr/lib
Address: http://www.cnblogs.com/xrq730/p/4827132.html
Http://blog.sina.com.cn/s/blog_48a45b9501015m6q.html
Differences between system. Load (string filename) and system. loadlibrary (string libname)