Helloworld example of JNI made using VC ++

Source: Internet
Author: User

JNI example

1. Introduction to JNI

JNI: Java Native Interface is a common interface provided by the Java language for interaction between Java code and localized code. Localized Code refers to machine-related Binary Code directly compiled, rather than intermediate code such as Java bytecode. In Windows, executable files, DLL files, and so files in Linux are binary code.
JNI allows Java programs to interoperate with libraries (DLL, so) or executable files written in other languages, including assembly, C, and C ++. JNI has the following requirements:
(1). Your application needs to use system-related functions, but Java code does not support or is difficult to do. This is a typical Implementation of the tray icon, there are several ready-made solutions are made using JNI, the name seems to be called trayicon and stayontop. Of course, if Java 1.6 is used, it should be another matter.
(2). There are libraries or programs written in other languages, and you want Java programs to use them.
(3). For higher performance requirements, we hope to use assembler or C/C ++ to implement some functions.
I will not talk much about other theoretical things. JNI tutorial is very clear. It is strongly recommended to read.

2. JNI development steps
Here we use C ++ to compile the localization method implementation as an example to develop a demo program using JNI. The specific steps are as follows:
(1). Compile a Java class WITH NATIVE METHOD
(2) Use the javac command to compile the compiled Java class
(3) Use the javah command to process class files and generate C/C ++ header files
(4). Use C/C ++ to implement local methods
(5) generate a dynamic connection library for files written in C/C ++

3. Let's start writing the helloworld program. We will use Microsoft Vc ++ in development because it involves writing C/C ++ code.

1. Write java code

Create a java file in the root directory of the c drive. The content is as follows:

class HelloWorld{    public native void displayHelloWorld();    static {        System.loadLibrary("hello");    }       public static void main(String[] args) {        new HelloWorld().displayHelloWorld();    }}

Note that System. the loadLibrary ("hello") code is defined in the static initialization block and is used by the system to load the hello shared library. This is the hello generated later. dll. so)

2. compile java code

Javac HelloWorld. java generates the HelloWorld. class File

3. Create a. h file

Javah HelloWorld

4. Write local implementation code

First, open VC ++ 6.0, choose "New"> "project"> "win32 Dynamic-Link Library", and select an empty project from the wizard.

Then copy the header file generated by javah to the newly created Project. Then, choose File> New> text file and create one by yourself. c file (must be added. c or. c ++ suffix), file name is hello, implement your native function. The list is as follows:

#include <jni.h>#include "HelloWorld.h"#include <stdio.h>JNIEXPORT void JNICALLJava_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj){    printf("Hello world!\n");    return;}

An error occurs when saving the compilation:

A. jni. h is missing. Jni. h is usually in jdk/include/. You can copy it to the include directory under the VC installation directory first. Once and for all, JNI technology can be easily used in the future.

B. jni_md.h is missing. Find jni_md.h in jdk \ include \ win32 and copy it to the c ++ project.

After compilation, a prompt can be prompted to define executable files. Select a. EXE file under the folder in the debagfile (the principle is not understood =, = ). In this way, the. dll file will be generated in the debug folder of the c ++ project. Copy

C. Enter the root directory in the command line.

Java HelloWorld

The result is displayed.

PS:# What is the difference between I nclude <file. h> and # I nclude "file. h?

The former searches for and references file. h from the path of the Standard Library, and the latter searches for and references file. h from the current working path.

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.