`

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.
分享到:
评论

相关推荐

    Advanced.Java.Programming.0199455503

    Java Native Interface Chapter 9. AWT and Swing Chapter 10. Java and XML Chapter 11. Input/Output PART II: NETWORK PROGRAMMING Chapter 12. Basic Networking Chapter 13. Socket Programming Chapter 14. ...

    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

    This tutorial deals with the two most common applications of JNI: calling C/C++ code from ...of the Java Native Interface and some of the more advanced programming challenges that can arise.

    java jni 教程 资料

    This chapter introduces the Java Native Interface (JNI). The JNI is a native programming interface. It allows Java code that runs inside a Java Virtual Machine (VM) to interoperate with applications ...

    java programming 5th edtion

    Java is popular among professional programmers because it can be used to build visually interesting graphical user interface (GUI) and Web-based applications. Java also provides an excellent ...

    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

    《C++ for Java Programmers》高清完整英文PDF版

    Explains the use of the powerful JNI (Java Native Interface) for combining Java and C++ Includes a summary of key C++ features and issues with each chapter Provides extensive treatment of C details ...

    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.

    java7帮助文档

    Java Virtual Machine Support for Non-Java Languages: Java SE 7 introduces a new JVM instruction that simplifies the implementation of dynamically typed programming languages on the JVM. Garbage-First...

    深入java虚拟机(inside the java virtual machine)

    The Java Programming Language Architectural Tradeoffs Future Trends On the CD-ROM The Resources Page 2 Platform independence Why Platform Independence? Java's Architectural Support for Platform ...

    Core Java 9th Edition(Vol1,Vol2)

    The Java Programming Environment Chapter 3. Fundamental Programming Structures in Java Chapter 4. Objects and Classes Chapter 5. Inheritance Chapter 6. Interfaces and Inner Classes Chapter 7. ...

    Java2核心技术卷I+卷2:基础知识(第8版) 代码

    Java As a Programming Platform 2 The Java “White Paper” Buzzwords 2 Java Applets and the Internet 7 A Short History of Java 9 Common Misconceptions about Java 11 Chapter 2: The Java ...

    java.核心技术.第八版 卷1

    Volume II–Advanced Features, covers advanced user-interface programming and the enterprise features of the Java SE 6 platform. Like Volume I (which covers the core language and library features), ...

    Asynchronous Android Programming

    Use the JNI interface to execute concurrent work on native layer Pull and push data from your network servers using the Google GCM platform Create and compose tasks with RxJava to execute complex ...

    JavaSE-6.0-英文手册(2008/11/30_FullUpdate)

    Java Native Interface (JNI) Security XML (JAXP) Integration Libraries Java Database Connectivity (JDBCTM) Java IDL Java Naming and Directory InterfaceTM (JNDI) Remote Method Invocation ...

    Android应用中即时视频图像放大显示方法研究 (2013年)

    为解决使用常用图像拉伸方式将解码到的视频图像实时放大输出时遇到的内存严重消耗和输出延迟的问题,分析了问题的原因,并研究了基于Android 自带API( Application Programming Interface)或使用JNI( Java Native ...

    Asynchronous Android Programming(PACKT,2ed,2016)

    Use the JNI interface to execute concurrent work on native layer Pull and push data from your network servers using the Google GCM platform Create and compose tasks with RxJava to execute complex ...

    Pro HTML5 Programming(HTML5高级程序设计英文原版)

    WebSockets and server-sent events provide not only a standard against which real-time web applications can be built, but also a socket, native to the browser, that facilitates network programming ...

    [iPhone开发书籍大全].Learn.Objective-C.on.the.Mac.pdf

    Take your coding skills to the next level with this extensive guide to Objective–C, the native programming language for developing sophisticated software applications for Mac OS X. Objective–C is a ...

Global site tag (gtag.js) - Google Analytics