Java calls methods in the.dll file

  • 2020-04-01 01:41:10
  • OfStack

Lower JNative component

(link: http://jnative.sourceforge.net/) to download JNative open source project, I download 1.3.2

Unpack the JNative - < St1 :chsdate isrocdate="False" islunardate="False" day="30" month="12" year="1899"> 1.3.2 < / st1: chsdate> .zip

Get three files: jnativecpp.dll, libjnativecpp.so, jnative.jar.
Jnativecpp. DLL for Windows, copy to Windows/system32 directory;
Libjnativecpp. so Linux, copy to the system directory;
Jnative-jar this is an extension package that will be automatically loaded when imported into the project LIB or copied to JDK \jre\ LIB \ext.

The & # 8226; Directions for use

My project will use the JNative component to call a testappvr.dll file that tests the state of the application server. The DLL file contains a TestConnect() method that returns a plastic (1 or 0)

First, configure the Windows environment of the JNative component:
Put Native under \WINDOWS/system32 on the system disk using jnativecpp.dll

Import jnative-jar into the project and create a new calling 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 AppSvrTestConnect {   

    public AppSvrTestConnect() {   

    }   

      
    private static final int TestConnect(String ip, int port, int intrcpt)throws NativeException, IllegalAccessException {   
        JNative n = null;   
        try {              
            n = new JNative("TestAppSvr.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();   
        }   
    }   
    /**  
     *  The specified Dll The file path , Dynamically loads the local link library , Test the application server connection status   
     * setDllPath  
     * @param path Dll Path to file , Does not contain DLL The name of the   Such as: windows - d:testtest unix - root/test/test/  
     * @param ip  Application server IP  
     * @param port  port   
     * @param intrcpt   Whether to use data compression mode  1 :true 0:false  
     * @return int 1 : successful  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);   
    }   
      
    public static final int TestConnectFromDllPath(String ip, int port, int intrcpt) throws NativeException, IllegalAccessException{   
        System.loadLibrary("TestAppSvr");   
        return TestConnect(ip,port,intrcpt);   
    }   
}

This class implements a static private method that calls a method in a Dll file to return the result

Private static final int TestConnect(String IP, int port, int intrcpt)

Two static public methods that load DLL files in two ways

Public static final int TestConnectFromDllPath(String path,String IP, int port, int intrcpt)  // the path through the DLL file
Public static final int TestConnectFromDllPath(String IP, int port, int intrcpt) //                       Then create a new class, call appsvrtestconnect.java, implement method one call, I am testappvr.dll file and demo.java in a directory , so get the Demo. The path of the Java after get TestAppSvr. DLL path, call AppSvrTestConnect. TestConnectFromDllPath () method can return the correct information. After the method 2 is already TestAppSvr. DLL in the Jre \ bin directory, when the JVM this will automatically load, and then through the System. LoadLibrary (" TestAppSvr ") can be assembled D LL documents.

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);   //Get the DLL's path & NBSP;  

        String ip = "192.168.0.48"; //Server IP   
        int port = 221;             //Port    
        int intrcpt = 1;            //Data compression transmission,1 is adopted; 0 does not use & cake;  
        //Method 1 pass in the path of Dll & NBSP;  
        //int info = AppSvrTestConnect.TestConnectFromDllPath(path, ip, port, intrcpt);   

        //Method 2 Dll file has been placed under the JRE  bin directory & NBSP;  
        int info = AppSvrTestConnect.TestConnectFromDllPath(ip, port, intrcpt);   

        //1 is success, 0 is failure & NBSP;  
        if (info == 1)   
            System.out.println(" Application servers are available. ");   
        else  
            System.out.println(" The application server is not available, please check IP Is the address and port correct? ");   

        return info;   
    }   

System.loadlibrary (): loads the local link library under Windows\System32 or under jre\bin or Tomcat\bin directory

System.load(): truncate the local link library according to the specific directory, must be the absolute path

The & # 8226; note

The above example project, because it is an example, so there is no most of the design, just the implementation of loading DLL file, DLL file method, return information.

For a detailed description of JNative, refer to the JNative source program and examples.

Note that the JVM only allows a default ClassLoader to load the native library and does not provide a dedicated API to unload a loaded native library, so start the Web Server independently while the project is debugging.


Related articles: