Load local libraries (. DLL files) in Java and eclipse)

Source: Internet
Author: User

Recently, the local method is used to load a lot of Dynamic Link Libraries in Java. (The following is a convenient extension of DLL on Windows platform, but it is not limited to Windows ). I just ran the program and quickly wrote some experiences, Mark. I also hope it will help you with similar work.

First, it should be clear that there are two types of DLL: (1) DLL and (2) DLL on which Java depends. It is precisely because of the existence of the (2) DLL that the complexity of loading the DLL in Java is greatly increased. Many statements are as follows, however, the results of my experiment show that the experiment is not so complex and will be elaborated later.

Second, there are two methods to load DLL in Java: (1) call system. loadlibrary (string filename) and (2) call system. Load (string filename. The underlying layer is implemented by using the loadlibrary (class fromclass, string name, Boolean isabsolute) method in classloader. The difference is that the filename in (1) must be an absolute path, (2) the filename in can only be a DLL name and cannot contain folders.

In addition, eclipse is a very powerful platform. It provides bundleclassloader strength, which is a very important reason. It also has its own unique approach to DLL loading, which deserves our adoption.

According to the above introduction, the main problems and solutions for loading DLL in Java are described in two parts.

1. Load the DLL in a general Java program

The DLL to be loaded is as follows:

Digitdll. dll
Dsivsacct. dll
Dsivscomm. dll
Dsivstrans. dll
Jbpack. dll
Xcodedll. dll
Imagedllctrl. dll
Yhfiche. dll
Yhocr. dll
Yhbill. dll
Tsealsvrdll. dll
Timg. dll
Timage. dll

Some methods in timage are called directly. All the methods that are directly or indirectly dependent on timage in the list should not only load all the DLL files, but also pay attention to the dependency between them, the dependent dll must be loaded first. Otherwise, the following error occurs: unsatisfiedlinkerror. Therefore, we should first clarify the dependencies between DLL. The above list has already been processed.

Next, set the JVM search path so that it can find your DLL. The JVM search path is determined by the system attribute of Java. Library. path. The default value is the path content in the system environment variable. Therefore, you can modify the PATH variable to set Java. library. path attribute (eclipse needs to be restarted after the change). The general method is to add the absolute path of the DLL folder to the path. Another method is to add "-djava. Library. Path = absolute path of the DLL folder" to the parameters of the Java command to set (you can use; Separate multiple paths ). For applications in the eclipse development environment, you can modify the startup parameters and add the preceding parameters to the VM arguments editing box. You can edit the application in the startup directory of the packaged eclipse installation package. INI (assuming that the startup file is application.exe), add the preceding parameter after-vmargs to set Java. library. PATH value. It should be noted that once the JVM has been started, the content of Java. Library. path cannot be modified, that is, through:

System. setproperty ("Java. Library. Path", "C:/mylib ");

This method cannot achieve the goal, because the attribute is read-only. Sun's forum once discussed how to modify java. Library. path in the Code. The conclusion is: The Code cannot be modified! If "Java-djava. library. path = C:/mylib "is too hard to write. It can only be used to pre-process the path through methods such as shell programming to improve its flexibility.

If your DLL is encapsulated in a jar package, first extract it to a temporary path, and then add the path to djava. library. or decompress it to the system path.

2. Load the DLL on the eclipse Platform

As mentioned above, the method for setting the local library path in Java is too dead, which is my own experience, however, we are pleased that our eclipse platform provides a set of flexible practices. Through the bundleclassloader provided by ECLIPSE, you can seal the DLL in the plugin, you do not need to decompress the file during use or set Java. library. path attribute. bundleclassloader will find your DLL in the specified directory relative to the plugin root directory. These directories are: WS/Win32/, OS/Win32/x86 /, OS/Win32/, NL/zh/CN/, NL/zh/, see Org. eclipse. osgi. internal. baseadaptor. defaultclassloader and org. eclipse. core. runtime. internal. adaptor. eclipseclassloadinghook.

My directory settings are:
. Classpath
. Cvsignore
. Project
Build. Properties
Classes
CVS
Lib
META-INF
OS
Plugin. xml
SRC

I put all the DLL files in the Win32 Directory under the OS. You can also create a directory such as WS/Win32 to place the local library. After such processing, the local database can be loaded without modifying any system variables.

In addition, eclipse also provides bundle-nativecode settings in the manifest file, which is also used to load local libraries. Further research is required.

This article is in a hurry and I hope it will be helpful to everyone.

This article from: (www.91linux.com) detailed source reference: http://www.91linux.com/html/article/program/java/20070901/6636.html

 

Related Article

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: info-contact@alibabacloud.com 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.