Summary of usage of system.loadlibrary ()

Source: Internet
Author: User

When using System.loadlibrary () to invoke a Dll, there are two ways:

1. Set environment variables.

For example, the edited DLL is in the directory "D:/cppprojects/nativecode/release", and the path is added to the computer's environment variable by adding it to the path variable.

2. Set project properties. (Development recommendation)

Right-click project Name | Select Properties properties| SELECT Java Build Path in the left list | On the Right tab select "Source" | "+" before the project name, select "Native Library location", "Edit" Select the "d:/cppprojects/nativecode/release" path above. (Of course, if you copy the DLL to workspace, you can also use a relative path.) You can also right-click "src" to set its properties within the native library. )

System.load and System.loadlibrary detailed

1. They can be used to load library files, either JNI or non-JNI library files. You must first load the corresponding JNI library file with one of the two methods before any local method is invoked.

The 2.system.load parameter is an absolute path to the library file, and can be any path.
For example, you can load a JNI library file in a Windows platform like this:
System.load ("C://documents and Settings//testjni.dll");.

3. The system.loadlibrary parameter is the library filename and does not contain the extension of the library file.
For example, you can load a JNI library file in a Windows platform like this
System. LoadLibrary ("Testjni");

Here, the TestJNI.dll must be in the path pointed to by the Java.library.path JVM variable.
You can obtain the value of the variable by:
System.getproperty ("Java.library.path");
By default, under the Windows platform, this value contains the following location:
1 Some directories related to JRE
2) Program Current directory
3) Windows directory
4) system catalog (SYSTEM32)
5 system environment variable path specified directory

4. If you want to load the library file statically linked to other dynamic link libraries, such as TestJNI.dll static link to Dependency.dll, then you must note:
1) If you choose
System.load ("C://documents and settings//TestJNI.dll");
So even if you put Dependency.dll in the same c://documents and settings//, load will fail because the dependent DLL is not found. Because the JVM is loading the TestJNI.dll first to load the library file Dependency.dll that TestJNI.dll relies on, Dependency.dll is not located in the directory specified by Java.library.path, so the JVM cannot find Dependency.d ll
You have two ways to solve this problem: one is to add c://documents and settings//to the Java.library.path path, for example, by adding it to the paths of the system. Second, the first call
System.load ("C://documents and settings//Dependency.dll"); Let the JVM load the Dependency.dll before invoking System.load ("C://documents and settings//TestJNI.dll");
2) If you choose
System. LoadLibrary ("Testjni");
Then you just put the Dependency.dll in any java.library.path included path, and of course it includes the same directory as TestJNI.dll.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.