`

Java Native Interface Programming

    博客分类:
  • Java
阅读更多

http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/index.html

 

Java Native Interface Programming

 

The JDK1.1 supports the Java Native Interface (JNI). On one hand, the JNI defines a standard naming and calling convention so that the Java Virtual Machine (VM) can locate and invoke your native methods. On the other hand, the JNI offers a set of standard interface functions. You call JNI functions from your native method code to do such things as access and manipulate Java objects, release Java objects, create new objects, call Java methods, and so on.

This section shows you how to follow the JNI naming and calling conventions, and how to use JNI functions from a native method. Each example consists of a Java program that calls various native methods implemented in C. The native methods, in turn, may call JNI functions to access the Java objects.

The section also shows you how to embed the Java Virtual Machine into a native application.

Declaring Native Methods

On the Java side, you declare a native method with the native keyword and an empty method body. On the native language side, you provide an implementation for the native method. You must take care when writing native methods to "match" the native function implementation with the method signature in Java. javah , explained in Step 3: Create the .h file , is a helpful tool to generate native function prototypes that match the Java native method declaration.

Mapping between Java and Native Types

The JNI defines a mapping of Java types and native (C/C++) types. This section introduces the native types corresponding to both primitive Java types (e.g., int , double ) and Java objects (including strings and arrays).

Accessing Java Strings

Strings are a particularly useful kind of Java objects. The JNI provides a set of string manipulation functions to ease the task of handling Java strings in native code. The programmer can translate between Java strings and native strings in Unicode and UTF-8 formats.

Accessing Java Arrays

Arrays are another kind of frequently-used Java object. You can use JNI array manipulation functions to create arrays and access array elements.

Calling Java Methods

The JNI supports a complete set of "callback" operations that allow you to invoke a Java method from the native code. You locate the method using its name and signature. You can invoke both static and instance (non-static) methods. javap is a helpful tool to generate JNI-style method signatures from class files.

Accessing Java Fields

The JNI allows you to locate the field using the field's name and type signature. You can get hold of both static and instance (non-static) fields. javap is a helpful tool to generate JNI-style field signatures from class files.

Catching and Throwing Exceptions

This section teaches you how to deal with exceptions from within a native method implementation. Your native method can catch, throw, and clear exceptions.

Local and Global References

Native code can refer to Java objects using either local or global references. Local references are only valid within a native method invocation. They are freed automatically after the native method returns. You must explicitly allocate and free global references.

Threads and Native Methods

This section describes the implications of running native methods in the multi-threaded Java environment. The JNI offers basic synchronization constructs for native methods.

Invoking the Java Virtual Machine

This section shows you how to load the Java Virtual Machine from a native library into a native application. This lesson includes how to initialize the Java Virtual Machine and invoke Java methods. The Invocation API also allows native threads to attach to a running Java Virtual Machine and bootstrap themselves into Java threads. Currently, the JDK only supports attaching native threads on Win32. The support for Solaris native threads will be available in a future release.

JNI Programming in C++

In C++, the JNI presents a slightly cleaner interface and performs additional static type checking.
分享到:
评论

相关推荐

    Practical Java Game Programming

    Chapter 11 - Java Native Interface Chapter 12 - 3D Graphics Foundations Chapter 13 - Java Bindings to OpenGL (JOGL) Chapter 14 - Overlays and Menus Using JOGL Chapter 15 - 3D Render Engine ...

    Java programming with JNI

    本教程主要关注Java Native Interface (JNI) 的使用。JNI是Java Software Development Kit (SDK) 的一部分,它允许Java代码调用用其他语言(如C和C++)编写的代码或库。通过JNI中的Invocation API,可以在本地应用...

    Java-NIO-Programming-Cookbook(含源码)

    直接缓冲区在Java中使用JNI(Java Native Interface)直接在物理内存中分配,而不需要经过Java堆,这通常能提高性能,尤其是在大量I/O操作时。 `DirectByteBufferExample.java`这个文件很可能是书中一个示例程序,...

    JAVA Programming Guide - Quick Reference

    - `interface`:接口声明。 - `long`:64位整型数据类型。 - `native`:声明原生方法。 - `new`:创建新对象。 - `null`:空引用。 - `package`:声明包。 - `private`:私有访问修饰符。 - `protected`:受保护的...

    Black Art of Java Game Programming

    Java提供了Java2D和JavaFX库来处理2D和3D图形,开发者可以学习如何绘制图像、动画,以及如何利用OpenGL通过JNI(Java Native Interface)进行高性能图形渲染。 3. **游戏框架**:书中可能会介绍如LibGDX、...

    Addison Wesley - programming for the java virtual machine.rar

    7. **JNI(Java Native Interface)**:Java与本地代码交互的接口,允许Java程序调用C/C++库。 8. **JIT(Just-In-Time)编译器**:JVM的一种优化技术,将热点代码编译为本地机器码以提高运行效率。 9. **类文件...

    JNI PROGRAMER

    ### Java Native Interface (JNI)详解 #### 一、引言 **Java Native Interface (JNI)** 是一种标准的应用程序编程接口(API),它允许Java代码与其他语言编写的代码进行交互,特别是与C和C++代码交互。JNI是Java平台...

    Java_programming_with_JNI

    Java程序设计与JNI手册详细介绍了Java Native Interface(JNI)的基础和常用技术,旨在帮助开发者快速有效地开发自己的JNI解决方案。JNI是Java软件开发工具包(SDK)的一部分,它允许Java代码调用用其他语言(如C和...

    Java中使用Jacob实现Windows下语音合成并生成音频文件示例代码.rar

    1. **Java Native Interface (JNI)**:JNI是Java平台的标准部分,它允许Java代码和其他语言写的代码进行交互。在Jacob中,JNI被用来连接Java虚拟机和Windows COM组件。 2. **Jacob库的使用**:首先需要将Jacob的jar...

    Java programming with JNI.pdf

    **Java Native Interface (JNI)** 是Java Software Development Kit (SDK) 的一部分,它提供了一个原生接口,使得Java代码能够调用其他语言(如C/C++)编写的代码或库。通过JNI中的Invocation API,可以在本地应用...

    Advanced Programming for the Java 2 Platform.chm

    JDBC Technology Servlets Chapter 5: Java Native Interface (JNI) Technology <br>JNI Example Strings and Arrays Other Programming Issues Chapter 6. Project Swing: Building a ...

    Java APIs, Extensions and Libraries, 2nd Edition--2018

    Extend your Java skills beyond the fundamental object-oriented concepts and ... Work with JavaFX, RMI (Remote Method Invocation), and JNI (Java Native Interface) Use the new scripting features of Java

    sap java connector jco 3.1.1.7z

    2. linux:这可能是一个目录,包含适用于Linux操作系统的SAP JCO native library,如libjco.so,它是Java Native Interface (JNI) 的一部分,用于处理与操作系统底层交互的部分,如网络通信和内存管理。 3. win:...

    Server Based Java Programming (Manning).pdf

    - **JNI概念**:Java Native Interface允许Java代码调用本地C/C++代码,从而实现更高的性能或者调用第三方库。 - **JNI开发注意事项**:由于涉及不同语言之间的交互,JNI开发需要注意内存管理、异常处理等问题。 ##...

    CS193-Advanced Java programming.rar

    4. **JNI(Java Native Interface)**:学习如何通过JNI与C/C++原生代码交互,以利用Java无法直接提供的系统功能或优化性能。 5. **内存管理和垃圾回收**:理解Java内存模型,包括堆内存、栈内存和方法区,以及垃圾...

    JAVA实现的TTS源代码

    本地方法是Java代码中使用`native`关键字声明的函数,它们与特定操作系统紧密关联,通常用C或C++编写,并通过Java的JNI(Java Native Interface)来调用。 在描述中提到的`native_method_src`文件夹,很可能包含了...

    Best practices for using JNI

    Best practices for using the Java Native Interface Techniques and tools for averting the 10 most common JNI programming mistakes.

    windows中JAVA对接SAP所需SDK

    它实现了SAP系统的远程调用功能,允许Java代码通过JNI(Java Native Interface)与SAP系统交互。在Windows系统中,Java程序不能直接调用C/C++编写的原生代码,因此需要这样的DLL来桥接Java和SAP的原生接口。 2. sap...

Global site tag (gtag.js) - Google Analytics