Java Invoke Hardware interface __java

Source: Internet
Author: User

Scenarios that use Java to invoke DLL dynamic-link libraries usually have three kinds: JNI, Jawin, Jacob. where JNI (Java Native

Interface) is a way for the Java language itself to invoke a locally compiled library of functions that is cross-platform and can be used in different

Different local libraries are invoked on the machine. Jawin and Jacob are sourceforge.net open source projects, all based on JNI technology dependent WI

The ndows implementation makes it easier to use COM and DLLs under the Windows platform.

One, JNI (Java Local method call)
The application scenarios for JNI are mapped based on Java classes and local functions. The steps to use a DLL are relatively cumbersome, not only involving

To Java programming, but also to C + + programming.

The steps to use JNI are:

1. Write a Java class that uses this class to declare a function service that the DLL provides externally, where the Java method is declared as native and its method

Signatures can be customized without implementing the function body.

2. Use the Javah tool to generate the corresponding. h header file for this Java class.

3. The most important and troublesome step: to write the functions declared in the C + + code implementation. h header file, which contains the jni.h header

, and programming the code with the data type that is defined as the input and return data type of the function. In this way

Implements data type conversions. In this step, you can call a DLL library that already exists in C + + code (when someone provides us with a current

DLL control, we only know the interface, we can't change its implementation, we need "custom DLL" to invoke "ready-made DLL".

4. You can use the Java class when you write another Java code.

In step 3rd, when you write a C + + function, you can use a env pointer called interface pointer to invoke a series of JNI-provided (

Many functions that use these functions to access the JVM's objects and data.

The disadvantage of using JNI: the use of a more cumbersome, need to encapsulate existing DLLs, need to understand C + + more.

Advantages of using JNI: You can invoke the local library across platforms.

Second, Jawin

Official website: http://jawinproject.sourceforge.net/

The application scheme of Jawin is based on the use of primitive byte stream to transmit data on function call. is to specify a letter in a DLL in Java

is passed to the DLL via the raw byte stream (the number of bytes stored in the parameter data type and the byte sequence used by the system)

function, and its return value is the correct value to be parsed by the original byte stream.

Jawin has a Jawin.dll and jawin.jar that require different configurations depending on the development environment.

Disadvantages of using Jawin: Inconvenient debugging, almost all errors throw the same exception comexception;

The conversion is more understanding; it is not cross-platform, and Windows is more dependent on it.

Advantages of using Jawin: Easy to use, do not need to carry out C/D + + development, without the original DLL encapsulation can be easily used.

I realize: I am using Jawin to invoke a single point of the DLL, multiple errors, the final debugging failed, and then view its official documents,

It is found to be unsupported for some COM calls.

Third, Jacob

Official documents: http://danadler.com/jacob/

Jacob is the abbreviation for Java-com Bridge and can also be used to invoke DLLs. The bottom layer is also a JNI implementation with Windows flat

Table Dependencies.

The control calls are quite handy, you can call COM and active, you can do it in a few minutes, it downloads a DLL and a jar package,

Put the DLL under win_home/system32/, and put the jar bag under the classpath of the project.

Here's how I call a single point DLL:

import com.jacob.com.*;      public class callsso {       private static Dispatch dif;      //Load single point control Ssocrypto.ssocrypt.1       static {           dif  = new dispatch ("Ssocrypto.ssocrypt.1");      }       //generates random code, calls a single point of interface Ssoradomgen, passing parameters are strplainstring      public static int  ssoradomgen (string strplainstring)       {             variant radom = dispatch.call (dif,  "SSORadomGen"),  new variant (strplainstring));             Return radom.getint ();      } //Fetch of encrypted string, call single point interface Ssoencrypt, pass parameter is strplainstring, Lngrandomnum,strsvrcert      public static string ssoencrypt (String strPlainString,                                                int lngrandomnum)       {          string strsvrcert = "1BQYR-YLGXV-QM439-CJSCS-TCN6V";           variant encrypt = dispatch.call (DIF,   "Ssoencrypt", new    Variant (strplainstring), New variant (Lngrandomnum),new  Variant (Strsvrcert));          return encrypt.getstring ();       } } 


Disadvantage: You cannot cross platforms and have a strong dependency on Windows.

Advantage: The invocation is quite handy, and it is easy to use without encapsulating the original DLL.

Summary: The above three methods are now commonly used Java Call DLL method, I feel the third kind of easy to use, of course, each have a gifted

Point, there are performance problems, I did not test the comparison, please try it yourself

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.