Java invoke intrinsic method

Source: Internet
Author: User

Let's start with a simple example: A Java program calls the intrinsic method, which then invokes the Win32 API function MessageBox () to display a graphical text box. This example will later be used with J/direct. If your platform is not Win32, simply include the following C header:
#include <windows.h>
Replace with:
#include <stdio.h>
and replace the call to MessageBox () with the call printf ().
The first step is to write out the Java code that declares the intrinsic method and its arguments:

Class Showmsgbox {public
  static void Main (String [] args) {
    Showmsgbox app = new Showmsgbox ();
    App. ShowMessage ("Generated with JNI");
  }
  Private native void ShowMessage (String msg);
  static {
    system.loadlibrary ("Msgimpl");
  }
}

Following the inherent method declaration, there is a static code block that calls System.loadlibrary () (which can be invoked at any time but more appropriately) system.loadlibrary () loads a DLL into memory and establishes a link to it. The DLL must be in your system path, or in a directory that contains Java class files. Depending on the platform, the JVM automatically adds the appropriate file name extension.

1. C-header file Builder: Javah
Now compile your Java source files and run Javah on the compiled. class file. Javah is available in version 1.0, but because we want to use the Java 1.1 JNI, we must specify the-jni parameters:
Javah-jni Showmsgbox
Javah reads the class file and generates a function prototype for each intrinsic method declaration in a C or C + + header file. The following is the output--showmsgbox.h source file (to meet the requirements of this book, slightly modified):

/* Do don't EDIT this FILE 
   -it is machine generated/
#include <jni.h>/
* Header for class Showmsgbox * *

#ifndef _included_showmsgbox
#define _included_showmsgbox
#ifdef __cplusplus
extern "C" {
# endif
*
 * Class:     showmsgbox
 * method:    showmessage
 * Signature: (ljava/lang/string;) V
/jniexport void Jnicall 
java_showmsgbox_showmessage
  (jnienv *, Jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif

From the "#ifdef_cplusplus" This preprocessing boot command can be seen, the file can be compiled by the C compiler, but also by the C + + compiler. The first #include command includes a jni.h--header file that defines the types that are used in the rest of the file; Jniexport and Jnicall are macros that are appropriately expanded to match the boot commands dedicated to the different platforms; JNIEnv, Jobject and jstring are JNI data type definitions.

2. Name management and function signing
Jni unifies the naming conventions of the inherent methods; This is important because it is part of the mechanism by which the virtual machines link Java calls to intrinsic methods. Fundamentally, all inherent methods are started with a "Java" followed by the Java method's name, and the underscore character is used as a delimiter. If the Java intrinsic method is "overloaded" (that is, naming duplicates), then the function signature is appended to the name. In the comments in front of the prototype, you can see the intrinsic signature. For more detailed information about naming rules and intrinsic method signatures, refer to the appropriate JNI documentation.

3. Implement your own DLL
at this point, all we have to do is write a C or C + + source file that contains the header file generated by Javah, and implement the intrinsic method, and then compile it to generate a dynamic link library. This part of the work is platform-related, so I assume that the reader already knows how to create a DLL. By calling a Win32 API, the following code implements the intrinsic approach. Subsequently, it compiles and links to a file named MsgImpl.dll:
 

#include <windows.h>
#include "ShowMsgBox.h"

BOOL apientry DllMain (HANDLE hmodule, 
  DWORD Dwreason, void** lpreserved) {return
  TRUE;
}

jniexport void Jnicall 
java_showmsgbox_showmessage (jnienv * jenv, 
  jobject this, jstring jmsg) {
  const char * MSG;
  msg = (*jenv)->getstringutfchars (jenv, jmsg,0);
  MessageBox (Hwnd_desktop, MSG, 
    "Thinking in Java:jni",
    MB_OK | mb_iconexclamation);
  (*jenv)->releasestringutfchars (jenv, jmsg,msg);
}

If you are not interested in Win32, just skip the MessageBox () call; The most interesting part is the code around it. The arguments passed to the intrinsic method are the gates that return to Java. The first argument is type JNIEnv, which contains all the hooks that are needed to recall the JVM (the next section is described in detail). Because the type of the method is different, the second argument has its own different meanings. For a non-static method like the one above (also called an instance method), the second argument is equivalent to the "this" pointer in C + +, and is similar to the Java "This": all references the object that invoked the intrinsic method. For the static method, it is a reference to a particular class object, and the method is implemented in that class object.
The remaining arguments represent the Java objects that are passed to the intrinsic method call. The main types are also passed in this form, but they are passed "by value".
In a later section, we're going to talk about how to access and control the JVM from within an intrinsic method, and to explain the above code in more detail.

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.