Use VC ++ components in Java

Source: Internet
Author: User

Introduction

JNI is my favorite Java programming framework. It allows you to freely use locally written code. For example, if you want to use Windows APIs (DLL) in your Java program, you need to use JNI.

The most popular feature of Java is its platform independence. However, sometimes this feature makes it difficult to integrate Java programs with local platforms.

For example, before adding a message to a database in our project, we use the message queue mechanism of msm q (Microsoft messaging que) to maintain the message (to avoid message loss ). Microsoft provides the msm q api, a Windows API. However, our program uses Java to process messages, and Java cannot directly access such an API (DLL ). Therefore, we decided to create an intermediate dll which can serve as a bridge between Java and VC ++ (the technology we used in Java Native inteface or JNI ).

Before further introduction, You should know or be familiar with the following knowledge:

1. local method: the local method is used. the method declared in the Java file, which will be defined in your local code (for Windows, in VC ++.

2. Static block: the static block in Java refers to the code block that can be executed before any other thing occurs.

Let's get started. First, use the local method declaration to write your Java program. The following code is from an example in the source code:

Public class jnitest
{
Static
{
System. loadlibrary ("jnitest"); // loading DLL in memory
}

Native void showmessage (string Str); // declaring NATIVE METHOD
Public jnitest ()
{
System. Out. println ("in the constructor of the Java program ");
}

Public static void main (string s [])
{
Jnitest jnt = new jnitest ();
Jnt. showmessage ("passing string from Java ");
}
}

In the above Code, we declare the showmessage method, which is a local method, and then calls it, the W. R. T class jnitest object.

Perform the following steps:

1. Compile the code to generate the. Class file.

2. Find the javah command in the JDK folder.

3. Run javah-JNI jnitest in the doscommand line.

After the above steps are completed, a jnitest. h file is generated. This. h file contains the function name in VC ++ of the local method declared in Java code.

Create JNI DLL

Half done.

Now, create a simple DLL project in Visual Studio (and any other Win32 IDE) and add the above. h file to the project.

To obtain the included file JNI. H, add the path of the JRE/include folder to your endpoints.

Now you will find that the names of Methods declared in the. Java file are slightly different.

In the above example, you will get:

Jniexport void jnicall java_jnitest_showmessage (jnienv *, jobject, jstring );

Here, the third parameter is your input from Java. You can change the form of its cost location as follows:

Const char * strs1 = env-> getstringutfchars (S1, 0 );

Here, you can use stes1 as a C ++ string in the program. I used it in the demo program and displayed it in MessageBox. There are many jnienv methods, through which we can convert multiple Java data types to C ++ data types.

After a string is used, it must be released, and the string will not be released for your own use. JNI behaviors are assumed to be JVM external behaviors, so it does not throw any type of exceptions that can be captured by Java code. If you forget to release the string before leaving the function, this may cause your JVM crash.

You can use the releasestringutfchars function to release a string:
Env-> releasestringutfchars (S1, strs1 );

The entire code may be like this:

Jniexport void jnicall java_jnitest_showmessage (
Jnienv * ENV, jobject job, jstring Str)
{
Const char * strmsgptr = env-> getstringutfchars (STR, 0 );
// Converting string to C ++ character pointer

MessageBox (0, strmsgptr, "message box from VC ++", 0 );
// Using the string

Env-> releasestringutfchars (STR, strmsgptr );
// Releasing the string (character pointer)
}

Now you can freely use Windows (or VC ++) components to do anything-now you can use Windows APIs.

Compile a DLL and put it in the Java program folder (this folder contains your. Class file ).

Run your Java program now.

Note:If you getUnsatisfiedlinkerrorCheck your method prototype. It must be the same as what you provide in the. h file.

With JNI, you can use many tools provided by windows in Java programs.

Download source files-14.1 KB

Download Demo project-10.3 KB

Prafullavedante

Click here to view prafullavedante's online profile.

 

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.