A detailed description of the use of JNI in Java first article: HelloWorld

Source: Internet
Author: User

Starting today to study the JNI technology, first or the old routine, output a HelloWorld: The specific process is as follows: Define a method in Java, implement this method in C + +, output "Hello world" inside the method, and then go back to Java to make the call. Divided into the following steps:

First step: Create a class in eclipse: Jnidemo

1 Package Com.jni.demo; 2  Public classJnidemo {3     //define a local method4      PublicNativevoidSayHello (); 5      Public Static voidMain (string[] args) {6         //invoke the dynamic-link library7System.loadlibrary ("Jnidemo"); 8Jnidemo Jnidemo =NewJnidemo (); 9 Jnidemo.sayhello (); Ten     }   One}

Where SayHello is the method to be implemented in C + +.

Step two: Use the Javah command to generate the Jnidemo. h header File:

The command is as follows:

E:\workspace\jnidemo\bin>javah Com.jni.demo.JNIDemo

Attention:

1. First make sure that the configuration of the Java environment variable is configured, otherwise the Javah command can not be used, specifically how to configure see: http://blog.csdn.net/jiangwei0910410003/article/details/17463173

2. My Java project is placed in the E:\workspace, so first go to the Project Bin directory, and then use the Javah command to generate the header file

3. Format of the class file after Javah: is the full name of the class (package name +class file name), and cannot have a. class suffix

When the command executes successfully, the header file is generated in the bin directory: com_jni_demo_jnidemo.h

However, we also need to pay attention to a problem, that is, if we include the native method of the class, if the reference to other places of the class, then this time into the Bin\classes\ directory will be a problem prompt can not find the specified class, then we need to switch to the source directory src run.

Step three: Generate the. dll file using VC6.0:

First create a DLL project:

Enter the following code in the. cpp file:

" com_jni_demo_jnidemo.h " void Jnicall Java_com_jni_demo_jnidemo_sayhello (jnienv * env, Jobject obj) {cout        <<"HelloWorld"<<Endl;  } </

Description

1. jniexport void Jnicall Java_com_jni_demo_jnidemo_sayhello (jnienv * env, Jobject obj)
{
cout<< "Hello World" <<endl;
}

The declaration of this method can be found in the Com_jni_demo_jnidemo.h header file generated above, which is the implementation of the SayHello method in Java Engineering

2. There are several issues with compiling here:

(1): You will be prompted not to find the corresponding header file:

In this case, the Jni.h,jni_md.h file needs to be tested in the project directory, the specific location of the two files in:

In the installation directory of Java, under the Include folder, jni_md.h this file in the Win32 folder, locate the two files, and then copy them to the C + + project directory;

(2) When copying to these two files, the compilation or the prompt cannot find the two files: The main reason is #include<jni.h> this is to find the jni.h header file from the system directory, and we only copy jni.h to the project directory, so we need to com_jni_ Change #include<jni.h> to # include "Jni.h" in the Demo_jnidemo.h header file, and change #include<jni_md.h> to # include in jni.h file Jni_ Md.h "

(3) There is also a mistake, hint: e:\c++\jnidemo\jnidemo.cpp (9): fatal error c1010:unexpected end of file while looking for precompiled Header directive, this is the precompiled header file read and write errors, this time also to set in the VC: Project-"Settings-" C + +; Select the precompiled header file in the classification, choose not to use the pre-compensated header:

In this way, the compilation succeeds, generating the JNIDemo.dll file in the Debug directory of the C + + project

Fourth step: Add the JNIDemo.dll file to the PATH environment variable:

Note: The path setting in the user variable is separated by semicolons: "; E:\c++\debug "so that the. dll file is added to the environment variable

Fifth step: Call the SayHello method in eclipse to output "Hello World": The Code is as follows:

 Public Static void Main (string[] args) {      // Call the dynamic-  link library    system.loadlibrary ("  Jnidemo");       New Jnidemo ();      Jnidemo.sayhello ();  }  

System.loadlibrary method is to load the JNIDemo.dll file, you must be careful not to have a. dll suffix name, only need the file name;

Note: When you run the error:

This hint is not found JNIDemo.dll file, this time we need to close eclipse, and then open, run there is no mistake, because eclipse each open to read the environment variable configuration, we just configured the path is not effective immediately, so to close eclipse, and then the new Open once.

Transferred from: http://blog.csdn.net/jiangwei0910410003/article/details/17465085

A detailed description of the use of JNI in Java first article: HelloWorld

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.