To meet project requirements, you need to call windows DLL (Dynamic Link Library) files in a Java project. It is troublesome to call the DLL files written in C using JNI, there are also many documents on the Internet. Finding an open-source component jnative on the Internet makes it easy to use.
-
Section jnative Components
Jnative.sourceforge.net/here to download the jnative open source project, I downloaded 1.3.2
-
ExtractJnative-<ST1: chsdate isrocdate = "false" islunardate = "false" Day = "30" month = "12" year = "1899"> 1.3.2 </ST1: chsdate>. zip
Obtain three files: jnativecpp. dll, libjnativecpp. So, and jnative. jar.
Jnativecpp. dll used in windows. Copy it to the Windows/system32 directory;
Libjnativecpp. So in Linux, copy it to the system directory;
Jnative. jar is an extension package. It is automatically loaded when it is imported to the Lib project or copied to JDK \ JRE \ Lib \ Ext.
-
Instructions for use
My project will use the jnative component to call a Test Application Server StatusTestbench VR. dllFile. The DLL file containsTestconnect ()Returns an integer (1 or 0)
First, configure the Windows environment of the jnative component:
Use nativeJnativecpp. dllPlaced on the System Disk\ Windows \ system32Lower
Import jnative. jar into the project and create a new call class:
Java Code
- Package com. tvjody;
- Import java. Io. file;
- Import java. Io. fileoutputstream;
- Import java. Io. ioexception;
- Import java. Io. inputstream;
- Import org. xvolks. jnative. jnative;
- Import org. xvolks. jnative. type;
- Import org. xvolks. jnative. Exceptions. nativeexception;
- Public class implements vrtestconnect {
- Public writable vrtestconnect (){
- }
- /**
- * Test the connection status of the application server.
- *
- * Testconnect
- * @ Param IP: IP address of the Application Server
- * @ Param Port port
- * @ Param intrcpt: whether to use data compression mode 1: True 0: false
- * @ Return int 1: Success 0: Failure
- * @ Throws nativeexception
- * @ Throws illegalaccessexception
- */
- Private Static final int testconnect (string IP, int port, int intrcpt) throws nativeexception, illegalaccessexception {
- Jnative n = NULL;
- Try {
- N = new jnative ("testw.vr. dll", "testconnect ");
- N. setretval (type. INT );
- Int I = 0;
- N. setparameter (I ++, type. String, ip );
- N. setparameter (I ++, type. Int, "" + port );
- N. setparameter (I ++, type. Int, "" + intrcpt );
- N. Invoke ();
- Return integer. parseint (N. getretval ());
- } Finally {
- If (n! = NULL)
- N. Dispose ();
- }
- }
- /**
- * Specify the DLL file path, dynamically load the local Linked Library, and test the connection status of the application server.
- * Setdllpath
- * @ Param path the DLL file path, excluding the DLL name, for example, Windows-D: \ test \ Unix-root/test/
- * @ Param IP: IP address of the Application Server
- * @ Param Port port
- * @ Param intrcpt: whether to use data compression mode 1: True 0: false
- * @ Return int 1: Success 0: Failure
- * @ Throws nativeexception
- * @ Throws illegalaccessexception
- */
- Public static final int testconnectfromdllpath (string path, string IP, int port, int intrcpt) throws nativeexception, illegalaccessexception {
- Path + = "testappsvr. dll ";
- System. Load (PATH );
- Return testconnect (IP, port, intrcpt );
- }
- /**
- * The DLL file is placed under the JRE \ bin directory, and the classloader can dynamically load the Local Link Library through system. loadlibrary ().
- * Testconnectfromdllpath
- * @ Param IP: IP address of the Application Server
- * @ Param Port port
- * @ Param intrcpt: whether to use data compression mode 1: True 0: false
- * @ Return int 1: Success 0: Failure
- * @ Throws nativeexception
- * @ Throws illegalaccessexception
- */
- Public static final int testconnectfromdllpath (string IP, int port, int intrcpt) throws nativeexception, illegalaccessexception {
- System. loadlibrary ("test1_vr ");
- Return testconnect (IP, port, intrcpt );
- }
- }
This class implements a static private method to call the methods in the DLL file to return results.
Private Static final int testconnect (string IP, int port, int intrcpt)
Two static public methods: Load DLL files in two ways
Public static final int testconnectfromdllpath (string path, string IP, int port, int intrcpt) // path through the DLL file
Public static final int testconnectfromdllpath (string IP, int port, int intrcpt) // use classloader
Create a new class and call systvrtestconnect. java, implementation method 1 call, I will test each VR. DLL file and demo. java is put in a directory, so we get the demo. test1_vr can be obtained after the Java path. dll path, call systvrtestconnect. the testconnectfromdllpath () method returns the correct information. method 2: testbench VR has been added. the DLL is stored in the JRE \ bin directory. It is automatically loaded when the JVM classloader is installed. loadlibrary ("test1_vr") can assemble DLL files.
Java code
- Public class demo {
- Public int getinfo () throws nativeexception, illegalaccessexception {
- String Path = getclass (). getresource (file. separator). getpath ();
- Path = path. substring (1, path. Length ());
- System. Out. println (PATH); // obtain the path of the DLL file.
- String IP = "192.168.0.48"; // server IP Address
- Int Port = 221; // Port
- Int intrcpt = 1; // data compression method, 1 is used; 0 is not used
- // Method 1: import the DLL file path
- // Int info = Using vrtestconnect. testconnectfromdllpath (path, IP, port, intrcpt );
- // Method 2 the DLL file is already under the JRE \ bin directory
- Int info = transport vrtestconnect. testconnectfromdllpath (IP, port, intrcpt );
- // 1 indicates success, 0 indicates failure
- If (Info = 1)
- System. Out. println ("application server available. ");
- Else
- System. Out. println ("the application server is unavailable. Check whether the IP address and port are correct. ");
- Return Info;
- }
System. loadlibrary ():Load the Local Link Library under windows \ system32, JRE \ bin, or Tomcat \ bin
System. Load ():Add a local link library according to the specific directory, which must be an absolute path
-
Remarks
In the example project above, because it is an example, there is not much design, but it only implements loading DLL files, calling the DLL file method, and returning information.
For more information about jnative, see the jnative source.ProgramAnd example.
Note that the JVM only allows one default classloader to load the native library, and does not provide special APIs to unload a loaded native library. Therefore, the web server is started independently during project debugging.