JNI technology Development and Configuration guide

Source: Internet
Author: User
Tags win32
1 Overview

This article is a guidance document for developing and configuring Java Local interface (Java Native interface,jni) technology. Includes: JNI introduction, loading mode, storage path, development steps, file configuration and so on. 2 jni introduction

JNI is a local programming interface. It enables Java code that runs within the Java Virtual Machine (VM) to interoperate with applications and libraries written in other programming languages such as C, C + +, and assembly language.

The most important benefit of JNI is that it does not impose any restrictions on the implementation of the underlying Java virtual machines. As a result, Java virtual machine vendors can add support for JNI without affecting other parts of the virtual machine. Programmers can work with all Java virtual machines that support JNI by writing only one version of the local application or library.

Chart 1

3 loading and linking local methods

The loading of local methods is implemented through the System.loadlibrary method. In the following example, the class initialization method loads a platform-related local library, giving the definition of a local method function in the local library:

package example;

public class Checkcode {

Native double function (int i, String s);

static {

System.loadlibrary ("Java_check_code");

}

}

The System.loadlibrary parameter is the library name that the programmer chooses arbitrarily. The system converts the library name to the local library name according to a standard but platform-related processing method. For example, the Solaris system converts the name Java_check_code to Libjava_check_code.so, and the WIN32 system converts the same name Java_check_code to Java_check_code.dll.

Programmers can use a single library to hold all the local methods that are required for any number of classes, as long as they are loaded by the same classloader. The virtual machine internally protects the list of local libraries that it loads for each class loader. Providers should try to choose a local library name that avoids name conflicts.

If the underlying operating system does not support dynamic linking, then all local methods must be linked to the virtual machine beforehand. In this case, the virtual machine does not actually need to load the library to complete the system.loadlibrary call.

The programmer can also call the JNI function registernatives () to register the local method associated with the class. The Registernatives () function is particularly useful when used with statically linked functions.

4 Library file placement path

The library file has a. dll suffix for the Win32 system filename, Linux/unix the system filename suffix. So. When using Java JNI technology, the JVM automatically finds the file to load according to the system path, and the library file should be placed under the system path.

1. For Windows systems, the system path path.

For example:

a) path=c:/windows/system32; C:/windows; C:/orcale10g2/bin; C:/apacheant/bin; C:/Program Files/java/jdk1.5.0_13/bin;d:/oracle/ora92/bin; C:/windows/system32; C:/windows; C:/windows/system32/wbem

2. For Linux systems,

For example:

A) ld_library_path= $LD _library_path:/usr/td/lib:/usr/ucblib:/usr/local/lib

b) ld_library_path= $LD _library_path: $USBOSS _home/lib: $HOME/dbci_ora/lib: $HOME/log4cpp/lib: $HOME/utility/lib : $HOME/smgp/lib: $HOME/ismap/lib 5 Development steps

Here is an example of how to write a localization method using C + +, as shown in the following steps:

(1). Writing Java classes with native methods

(2). Compiling the Java class written using the Javac command

(3). Use the Javah command to process class files and generate C + + header files

(4). Implementing local methods using C/C + +

(5). To generate a dynamic connection library for a file written by A/C + + 6 Frequently asked Questions 6.1 library files (. dll, . So) does not load properly.

System.loadlibrary ("Java_check_code");

for Win32 systems, the library file name for the program load should be: Java_check_code.dll, and if for the Linux/unix system, the library file name should be: libjava_check_code.so. 7 reference materials

1. Java local Interface Specification http://www.iplab.cs.tsukuba.ac.jp/~liuxj/jdk1.2/zh/docs/guide/jni/spec/design.doc.html

2. Java and C + +:: Summary on JNI Chinese string manipulation problem HTTP://DEV.CSDN.NET/DEVELOP/ARTICLE/17/17157.SHTMVVVS

3.

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.