Getting started with JNI Development

Source: Internet
Author: User
Getting started with JNI Development

 

I. Overview

JNI (Java Native Interface) is called by the Java Local interface to allow Java to call local programs. The main advantages include:
  • Improve efficiency and solve performance problems
  • Prevents decompilation and improves core code security
  • Enhance local interface call
  • Embedded Development and Application
The following is a simple demo to demonstrate how to develop JNI. Ii. Demo 1. Create a Java FileCreate a Java class as follows: Package CHB. test. JNI; <br/> public class hellojni {<br/> Public native void sayhello (); </P> <p> static {<br/> system. loadlibrary ("hellojni"); <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> New hellojni (). sayhello (); <br/>}< br/>}Location of hellojni. Java on Hard Disk: D:/workspace/mywork/src/CHB/test/JNI/hellojni. Java 2. Generate the class file1) Use the javac command to compile and enter the D:/workspace/mywork/src/CHB/test/JNI directory. Javac hellojni. Java2) use IDE, for example, I use eclipse for development and automatic compilation. The compiled file is located in the Directory D:/workspace/mywork/bin/CHB/test/JNI. 3. Generate the C header file1) if the javac command is used for compiling, enter the D:/workspace/mywork/src directory and enter Javah-jni chb. Test. JNI. hellojniThe new header file chb_test_jni_hellojni.h is found in the Directory D:/workspace/mywork/src. The file content is as follows: /* Do not edit this file-it is machine generated */<br/> # include <JNI. h> <br/>/* Header for class chb_test_jni_hellojni */<br/> # ifndef _ break <br/> # DEFINE _ break <br/> # ifdef _ cplusplus <br/> extern "C" {<br/> # endif <br/>/* <br/> * class: chb_test_jni_hellojni <br/> * method: displayhellojni <br/> * Signature :() v <br/> */<br/> jniexport void jnicall java_chb_test_jni_hellojni_displayhellojni <br/> (jnienv *, jobject ); <br/> # ifdef _ cplusplus <br/>}< br/> # endif <br/> # endif2) If IDE is used for compiling, you can directly use the javah command in the compiling root directory, for example, enter D:/workspace/mywork/bin, and then use Javah-jni chb. Test. JNI. hellojniThe results are the same. Note:: Because hellojni is in CHB. test. in the JNI package, you must specify the full path. Otherwise, if you directly go to the Directory D:/workspace/mywork/bin/CHB/test/JNI and run javah hellojni, an exception is reported, as follows: D:/workspace/ctcc/mywork/src/CHB/test/JNI> javah hellojni <br/> error: unable to access hellojni <br/> error class file :. /hellojni. class <br/> the class file contains the wrong class: CHB. test. JNI. hellojni <br/> delete the file or make sure the file is in the correct class path subdirectory. <Br/> COM. sun. tools. javac. util. abort <br/> at com. sun. tools. javac. comp. check. completionerror (check. java: 164) <br/> at com. sun. tools. javadoc. docenv. loadclass (docenv. java: 149) <br/> at com. sun. tools. javadoc. rootdocimpl. <init> (rootdocimpl. java: 77) <br/> at com. sun. tools. javadoc. javadoctool. getrootdocimpl (javadoctool. java: 159 <br/>) <br/> at com. sun. tools. javadoc. start. parseandexecute (start. java: 330) <br/> at com. sun. tools. javadoc. start. begin (start. java: 128) <br/> at com.sun.tools.javadoc.main.exe cute (main. java: 66) <br/> at com. sun. tools. javah. main. main (main. java: 147) <br/> javadoc: Error-fatal error <br/> 2 error  4. Create a C implementation and generate a DLL fileCreate a C Project and use Dev C ++. The source code is as follows: # Include <windows. h> <br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # include <JNI. h> <br/> # include "hellojni. H "<br/> jniexport void jnicall java_chb_test_jni_hellojni_sayhello (jnienv * a, jobject B) <br/>{< br/> printf (" Hello JNI! /N "); <br/> return; <br/>}The method is simple. Output a sentence, compile the project, generate hellojni. dll, and copy hellojni. DLL to the Windows/system32 directory. 5. execute Java codeRun the main method of hellojni and output "Hello JNI". The operation is successful. This article is relatively simple, mainly to help you get started with the first demo and have a perceptual knowledge.

 

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.