Using VC + + components in Java

Source: Internet
Author: User
Introduced
JNI is my favorite Java programming framework; it gives you the freedom to use locally written code. For example, if you want to use the Windows API (as a DLL) in your Java program, then you need to use JNI.

The most popular feature of Java is its platform-independent nature. However, sometimes this feature can make it difficult for you to integrate Java programs with your local platform.

For example, in our project, we used the queue mechanism of MSM Q (Microsoft messaging Que) to maintain messages (to avoid message loss) before adding messages to the database. Microsoft provides the MSM Q API, which is the Windows API. But our program uses Java to process messages, and Java does not have direct access to such APIs (DLLs). So we decided to make an intermediate DLL, which can be used as a bridge between Java and VC + + (we used technical Java Native inteface or JNI).

Before further introductions, you should know or familiarize yourself with some of the following knowledge:

1. Local method: The local method is one used in the. The method declared in the Java file, and the declaration will be defined in your local code (in VC + + for Windows).

2. Static BLOCK: A static block in Java refers to a block of code that can be executed before anything else happens.

So let's get started. First, write your Java program with a local method declaration. The following code comes 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's 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 the local method, and then calls it, W.R.T class Jnitest object.

Perform the following steps:

1. Compile code generation. class file

2. Locate the Javah command in the JDK folder

3. Execute javah-jni jnitest on DOS command line

Completing the steps above will result in jnitest. h files. this. h file contains a function name in VC + + of the local method declared in Java code.

Create a JNI DLL

I've finished half of it.

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.