Example of using VC + + 6.0 to achieve JNI

Source: Internet
Author: User

JNI is part of the JDK and is used to provide Java with a local code interface. Programs written with JNI can ensure that your code is fully ported to all platforms. JNI enables Java code that runs on a JVM virtual machine to manipulate applications and libraries written in other languages, such as C + + and assembly language. In addition, some of the APIs provided by JNI allow you to embed the JVM in your local application. The following figure expresses the role that JNI plays.

This article will illustrate the complete process of using vc++6.0 to implement JNI through an example. The steps to consolidate native code and Java code using JNI are certain, there is no room for further authoring, so readers can step through the steps to realize that Java is also "nothing can".

The implementation task description of JNI: Invoke the message box function under Windows in Java, and pass a string as the display text parameter of the MessageBox function in Java, displayed in the middle of the message box. Let's go into this wonderful journey together.

Step 1: Write a Java class that contains a description of the local method that needs to be invoked.

//WinMsgBox.java
package edu.netcom.jni;
public class WinMsgBox 
{
  static{
     System.loadLibrary("WinMsgDll");  // (1)
  }
  public native void showMsgBox(String str); // (2)
}

(1) Winmsgdll is the file name of the dynamic link file without extension, because the dynamic link file name extension is different under different platforms, automatically recognized by the JVM, such as under Solaris, and converted to winmsgdll.so in WIN32 environment. WinMsgDll.dll. This file name must be the same as the file name generated in step 4. The location of this file is also important, and it can only be placed in the folder specified in the JVM attribute value Java.library.path. This property value can be viewed using System.getproperty ("Java.library.path"); Typically, at least a few of these locations are determined to be reliable, under the Windows installation directory under System32, under the JDK installation directory under Bin, and the current directory where the main class file is invoked.

(2) Indicates a method that you must implement in local code.

Step 2: Command javac-d is used at the prompt. Winmsgbox.java compiles the Java files written in step 1.

A EDU\NETCOM\JNI directory structure is established in the current directory, and a Winmsgbox.class file exists.

Step 3: The command Javah-jni Edu.netcom.jni.WinMsgBox is used at the prompt, and a edu_netcom_jni_winmsgbox.h file is generated in the current directory, noting that the filename is made up of (package name + class name). The middle is separated by (_). The contents of this document are as follows:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>            // (1)
/* Header for class edu_netcom_jni_WinMsgBox */
#ifndef _Included_edu_netcom_jni_WinMsgBox
#define _Included_edu_netcom_jni_WinMsgBox
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:   edu_netcom_jni_WinMsgBox
* Method:  showMsgBox
* Signature: (Ljava/lang/String;)V    // (2)
*/
JNIEXPORT void JNICALL Java_edu_netcom_jni_WinMsgBox_showMsgBox
  (JNIEnv *, jobject, jstring);      // (3)
#ifdef __cplusplus
}
#endif
#endif

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.