Java Intrinsic interface Jni (Java Native Interface) HelloWorld

Source: Internet
Author: User

1. Writing Java programs

public class HelloWorld {public    native void Displayhelloworld ();//All native keywords are decorated with local declarations    static {        System.loadlibrary ("Hello");//load local library    } public    static void Main (string[] args) {        new HelloWorld (). Displayhelloworld ();    }}

2. Compiling

Javac Helloworld.java

3. Generate a header file with the extension h

Javah HelloWorld

4. Write a method with the same method name declared in the local method implementation and the header file generated by the Javah command.

#include "jni.h" #include "HelloWorld.h"//#includeotherheaders Jniexport void Jnicalljava_helloworld_ Displayhelloworld (Jnienv*env,jobject obj) {printf ("helloworld!\n"); return;}

5. Write Pro file

TARGET = Hellotemplate = Liblanguage = c++config-= qtconfig + Warn_off dllincludepath     + = $$ (java_home) \include$$ (JAV A_home) \include\win32headers = Helloworld.hsources = helloworldimpl.c

6. Compiling dynamic link library

Qmakenmake

7. Running the program

Java HelloWorld

Can be output from the console.

Java Intrinsic interface Jni (Java Native Interface) HelloWorld

Related Article

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.