Java method for invoking the. dll file _java

Source: Internet
Author: User

Lower section jnative component

jnative.sourceforge.net/here to download jnative Open source project, I downloaded is 1.3.2

Extract the Jnative-<st1:chsdate isrocdate= "false" Islunardate= "false" day= "month=" and "A" year= "1899" >1.3.2</st1: Chsdate>.zip

Get three files, respectively: Jnativecpp.dll,libjnativecpp.so,jnative.jar.
JNativeCpp.dll under Windows, copy to Windows/system32 directory;
Libjnativecpp.so under Linux, copy to the system directory;
Jnative.jar This is an expansion pack, imported into the project Lib or copied to Jdk\jre\lib\ext, the system will automatically load.

• Instructions for use

My project will use the Jnative component to invoke a TestAppSvr.dll file that tests the state of the application server, and the DLL file contains a Testconnect () method that returns the result of an integer (1 or 0)

First configure the Windows environment for the Jnative component:
To use the native to JNativeCpp.dll on the \windows\system32 of the system disk

To import Jnative.jar into the project, create a new calling class:

Java code

Copy Code code as follows:

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 () {

}

/**
* Test Application Server Connection status
*
* Testconnect
* @param IP Application Server IP
* @param 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 DLL file path, dynamically load local link library, test application Server connection status
* Setdllpath
* @param path of the path DLL file that does not contain DLL names such as: Windows-d:\test\test\ unix-root/test/test/
* @param IP Application Server IP
* @param 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, Illeg alaccessexception{
Path = = "TestAppSvr.dll";
System.load (path);
Return Testconnect (IP,PORT,INTRCPT);
}
/**
* DLL files are placed under the Jre\bin directory, ClassLoader can dynamically load the local link library via System.loadlibrary ()
* Testconnectfromdllpath
* @param IP Application Server IP
* @param 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, Illegalaccessexce ption{
System.loadlibrary ("Testappsvr");
Return Testconnect (IP,PORT,INTRCPT);
}
}

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

private static final int testconnect (String IP, int port, int intrcpt)

Two static public methods to load 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 app Svrtestconnect.java, implementation method One call, I am the TestAppSvr.dll file and Demo.java placed in a directory, so get Demo.java path can get TestAppSvr.dll path, The Appsvrtestconnect.testconnectfromdllpath () method is invoked to return the correct information. Method Two is already placed TestAppSvr.dll in the Jre\bin directory, It is automatically loaded when the JVM is ClassLoader, and then the DLL file can be assembled by system.loadlibrary ("Testappsvr").

Java code

Copy Code code as follows:

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 path to the DLL file

String IP = "192.168.0.48"; Server IP
int port = 221; Port
int intrcpt = 1; Data compression mode transmission, 1 for adoption; 0 for not using
Method 1 path to incoming DLL file
int info = appsvrtestconnect.testconnectfromdllpath (path, IP, port, INTRCPT);

Method 2 DLL files are already placed under the Jre\bin directory
int info = Appsvrtestconnect.testconnectfromdllpath (IP, port, INTRCPT);

1 for success, 0 for failure
if (info = 1)
SYSTEM.OUT.PRINTLN (the application server is available.) ");
Else
SYSTEM.OUT.PRINTLN (the application server is not available, check that the IP address and port are correct.) ");

return info;
}

System.loadlibrary (): Load local link library under Windows\System32 or Jre\bin or Tomcat\bin directory

System.load (): The local link library is truncated according to the specific directory, and must be an absolute path

• Notes

The above example works, because is an example, so there is no most design, just implemented the loading DLL file, calling DLL file method, return information.

For more information on jnative, please refer to the source program and examples of jnative.

Note that the JVM allows only one default ClassLoader to load the native library, and does not provide a dedicated API to unload a loaded native library, so when the project is debugged, the web is launched independently Server.

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.