Below is the JNative component
jnative.sourceforge.net/ Go here to download the JNative open source project. I downloaded 1.3.2
Unzip JNative-<st1:chsdate isrocdate="False" islunardate="False" day="30" month="12" year="1899">1.3.2</st1:chsdate>.zip
Obtain three files, namely: JNativeCpp.dll, libJNativeCpp.so, JNative.jar.
JNativeCpp.dll is used under Windows, copy it to the windows/system32 directory;
libJNativeCpp.so under Linux, copy to the system directory;
JNative.jar This is an extension package. Import it into the project LIB or copy it to jdk/jre/lib/ext, and the system will automatically load it.
•Instructions for use
My project will use JNative components to call a TestAppSvr.dll file that tests the status of the application server. The Dll file contains a TestConnect() method that returns an integer result (1 or 0)
First configure the windows environment of the JNative component:
Place the JNativeCpp.dll that Native needs to use under /WINDOWS/system32 on the system disk
Import JNative.jar into the project and create a new calling class:
java code
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() {
}
/**
* Test application server connection status
*
*TestConnect
* @param ip application server IP
* @param port port
* @param intrcpt Whether to use data compression method 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("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();
}
}
/**
* Specify the Dll file path, dynamically load the local link library, and test the application server connection status
* setDllPath
* @param path The path to the Dll file, excluding the DLL name. For example: windows - d:/test/test/ unix - root/test/test/
* @param ip application server IP
* @param port port
* @param intrcpt Whether to use data compression method 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 ClassLoader can dynamically load the local link library through System.loadLibrary()
* TestConnectFromDllPath
* @param ip application server IP
* @param port port
* @param intrcpt Whether to use data compression method 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("TestAppSvr");
return TestConnect(ip,port,intrcpt);
}
}
This class implements a static private method, which is used to call methods in Dll files and return results.
private static final int TestConnect(String ip, int port, int intrcpt)
Two static public methods, loading DLL files in two ways
public static final int TestConnectFromDllPath(String path,String ip, int port, int intrcpt) //Path through DLL file
public static final int TestConnectFromDllPath(String ip, int port, int intrcpt) //Through ClassLoader and then create a new class, call AppSvrTestConnect.java, implement method one call, I put the TestAppSvr.dll file and Demo.java in the same directory , so after getting the path of Demo.java, you can get the path of TestAppSvr.dll, and calling the AppSvrTestConnect.TestConnectFromDllPath() method can return the correct information. The second method is to place TestAppSvr.dll in the Jre/bin directory. It will be automatically loaded in the JVM's Classloader, and then the DLL file can be assembled through System.loadLibrary("TestAppSvr").
java code
String path=getClass().getResource(File.separator).getPath();
path = path.substring(1,path.length());
System.out.println(path); //Get the path of the DLL file
String ip = "192.168.0.48"; //Server IP
int port = 221; //port
int intrcpt = 1; //Data compression is transmitted, 1 means using it; 0 means not using it
//Method 1 Pass in the path of the Dll file
//int info = AppSvrTestConnect.TestConnectFromDllPath(path, ip, port, intrcpt);
//Method 2 The Dll file has been placed under the JRE/bin directory
int info = AppSvrTestConnect.TestConnectFromDllPath(ip, port, intrcpt);
//1 is success, 0 is failure
if (info == 1)
System.out.println("Application server is available.");
else
System.out.println("The application server is unavailable, please check whether the IP address and port are correct.");
return info;
}
System.loadLibrary(): Load the local link library under Windows/System32 or jre/bin or Tomcat/bin directory
System.load(): Add the local link library according to the specific directory, which must be an absolute path
•Remark
The above example project, because it is an example, does not have much design. It only implements loading the DLL file, calling the DLL file method, and returning information.
For detailed instructions on JNative, please refer to the JNative source program and examples.
Note that the JVM only allows a default ClassLoader to load native library, and does not provide a special API to unload a loaded native library, so when debugging the project, start the Web Server independently.