Java calls the. dll file

Source: Internet
Author: User

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
    1. Package com. tvjody;
    2. Import java. Io. file;
    3. Import java. Io. fileoutputstream;
    4. Import java. Io. ioexception;
    5. Import java. Io. inputstream;
    6. Import org. xvolks. jnative. jnative;
    7. Import org. xvolks. jnative. type;
    8. Import org. xvolks. jnative. Exceptions. nativeexception;
    9. Public class implements vrtestconnect {
    10. Public writable vrtestconnect (){
    11. }
    12. /**
    13. * Test the connection status of the application server.
    14. *
    15. * Testconnect
    16. * @ Param IP: IP address of the Application Server
    17. * @ Param Port port
    18. * @ Param intrcpt: whether to use data compression mode 1: True 0: false
    19. * @ Return int 1: Success 0: Failure
    20. * @ Throws nativeexception
    21. * @ Throws illegalaccessexception
    22. */
    23. Private Static final int testconnect (string IP, int port, int intrcpt) throws nativeexception, illegalaccessexception {
    24. Jnative n = NULL;
    25. Try {
    26. N = new jnative ("testw.vr. dll", "testconnect ");
    27. N. setretval (type. INT );
    28. Int I = 0;
    29. N. setparameter (I ++, type. String, ip );
    30. N. setparameter (I ++, type. Int, "" + port );
    31. N. setparameter (I ++, type. Int, "" + intrcpt );
    32. N. Invoke ();
    33. Return integer. parseint (N. getretval ());
    34. } Finally {
    35. If (n! = NULL)
    36. N. Dispose ();
    37. }
    38. }
    39. /**
    40. * Specify the DLL file path, dynamically load the local Linked Library, and test the connection status of the application server.
    41. * Setdllpath
    42. * @ Param path the DLL file path, excluding the DLL name, for example, Windows-D: \ test \ Unix-root/test/
    43. * @ Param IP: IP address of the Application Server
    44. * @ Param Port port
    45. * @ Param intrcpt: whether to use data compression mode 1: True 0: false
    46. * @ Return int 1: Success 0: Failure
    47. * @ Throws nativeexception
    48. * @ Throws illegalaccessexception
    49. */
    50. Public static final int testconnectfromdllpath (string path, string IP, int port, int intrcpt) throws nativeexception, illegalaccessexception {
    51. Path + = "testappsvr. dll ";
    52. System. Load (PATH );
    53. Return testconnect (IP, port, intrcpt );
    54. }
    55. /**
    56. * The DLL file is placed under the JRE \ bin directory, and the classloader can dynamically load the Local Link Library through system. loadlibrary ().
    57. * Testconnectfromdllpath
    58. * @ Param IP: IP address of the Application Server
    59. * @ Param Port port
    60. * @ Param intrcpt: whether to use data compression mode 1: True 0: false
    61. * @ Return int 1: Success 0: Failure
    62. * @ Throws nativeexception
    63. * @ Throws illegalaccessexception
    64. */
    65. Public static final int testconnectfromdllpath (string IP, int port, int intrcpt) throws nativeexception, illegalaccessexception {
    66. System. loadlibrary ("test1_vr ");
    67. Return testconnect (IP, port, intrcpt );
    68. }
    69. }

    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
  1. Public class demo {
  2. Public int getinfo () throws nativeexception, illegalaccessexception {
  3. String Path = getclass (). getresource (file. separator). getpath ();
  4. Path = path. substring (1, path. Length ());
  5. System. Out. println (PATH); // obtain the path of the DLL file.
  6. String IP = "192.168.0.48"; // server IP Address
  7. Int Port = 221; // Port
  8. Int intrcpt = 1; // data compression method, 1 is used; 0 is not used
  9. // Method 1: import the DLL file path
  10. // Int info = Using vrtestconnect. testconnectfromdllpath (path, IP, port, intrcpt );
  11. // Method 2 the DLL file is already under the JRE \ bin directory
  12. Int info = transport vrtestconnect. testconnectfromdllpath (IP, port, intrcpt );
  13. // 1 indicates success, 0 indicates failure
  14. If (Info = 1)
  15. System. Out. println ("application server available. ");
  16. Else
  17. System. Out. println ("the application server is unavailable. Check whether the IP address and port are correct. ");
  18. Return Info;
  19. }

 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.
       

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.