Java Local Method __java

Source: Internet
Author: User
What is a local method

In Java, there are methods that involve interacting with the operating system, acquiring and using the hardware or software of the operating system, simply using Java implementations that are difficult or impossible to implement, using local methods, local methods written in other languages, and then calling in the JVM Definition of local method

Local methods are seen in many classes, such as the Clone method of the object class

Protected native Object clone () throws Clonenotsupportedexception;

So when you use the Clone method, you actually call the C language. when to use the local method your application needs access to the system features and devices that cannot be implemented through the Java platform you've got a lot of tested and debugged code written in another language, and know how to export it to all target platforms through benchmarking, you've found that the Java code you write is much slower than the code written in other languages.

The Java platform has an API for interacting with local C code, called the Java Local interface (JNI), which calls C functions from Java programs

First define a local method

public class Hellonative {public

    static native void greeting ();

}

C function naming rules for local methods
1. Use the complete Java method name, such as Hellonative.greeting. If the class belongs to a package, add the registration, such as Com.hehe.HelloNative.greeting
2. Use the underline to replace all the periods and add the Java_ prefix, for example, java_hellonative_greeting
3. If the class name contains non-ASCII letters or numbers, such as ' _ ' or Unicode characters greater than ' \u007f ', replace them with _0XXXX, XXXX is a sequence of 4 hexadecimal numbers for the Unicode value of the character
Java provides a tool Javah to generate a header file of the local C function, which contains the declaration of the local function, which we can use
Compile hellonative First

Javac Hellonative.java

Then use Javah to generate the C header file

Javah hellonative

Generate a HelloNative.h

/* Do isn't EDIT this file-it is machine generated/*
#include <jni.h>/
* Header for class hellonative */
   #ifndef _included_hellonative
#define _included_hellonative
#ifdef __cplusplus
extern "C" {
# endif
*
 * Class:     hellonative
 * Method:    Greeting
 * Signature: () V
* * jniexport void Jnicall java_hellonative_greeting
  (jnienv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

Write hellonative.c using vc++6.0


Compile to System library
The jni.h in the HelloNative.h above is the JDK header file, so it's included when compiling

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.