首页 > 编程语言 >Java Native Interface Programming

Java Native Interface Programming

时间:2023-06-02 21:40:03浏览次数:63  
标签:Java methods Programming objects Interface JNI method native


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.

标签:Java,methods,Programming,objects,Interface,JNI,method,native
From: https://blog.51cto.com/u_16125990/6405066

相关文章

  • Java的可见性和原子性
    1.工作内存和主内存2.可见性和共享变量3.约束4.共享变量可见性的原理4.1可见性的步骤4.2保证可见性的必要条件synchronized实现可见性5.1synchronized实现的内容5.2JMM关于synchronized的规定5.3synchronized互斥代码的过程6volatile实现可见性6.1重排序6.2asifseri......
  • 构建服务器集群感知的 Java 应用程序
    ApacheZooKeeper和LinkedInProjectNorbert在分布式企业Java应用程序中简化服务器组协调服务器集群对于高度可扩展的Java企业级应用程序开发已司空见惯,但是应用程序级别的服务器集群感知目前并不属于JavaEE的一部分。在本文中,MukulGupta和PareshPaladiya向您介......
  • Google Java编程风格指南
    作者:Hawstein目录前言源文件基础源文件结构格式命名约定编程实践Javadoc后记前言这份文档是GoogleJava编程风格规范的完整定义。当且仅当一个Java源文件符合此文档中的规则,我们才认为它符合Google的Java编程风格。与其它的编程风格指南一样,这里所讨论的不仅仅是编码格......
  • Java编译与反编译
    Java编译与反编译什么是编译利用编译程序从源语言编写的源程序产生目标程序的过程。用编译程序产生目标程序的动作。编译就是把高级语言变成计算机可以识别的2进制语言,计算机只认识1和0,编译程序把人们熟悉的语言换成2进制的。编译程序把一个源程序翻译成目标程序的工作过程......
  • 40基于java的美食菜谱分享系统设计与实现
    本章节给大家带来一个基于java的美食菜谱分享系统设计与实现,餐饮分享平台设计与实现,可用于美食在线分享平台,作为世界各地爱好美食的人们的桥梁,为其创造一个氛围好的平台,促进美食世界的文化交流。该系统是一个供商家或者个人推荐美食的网站,网站不支持交易仅供分享。引言在21世......
  • [Javascript] Microtasks exec order
    button.addEventListener('click',(event)=>{console.log('listener1')queueMicrotask(()=>{console.log('microtask')})})button.addEventListener('click&#......
  • Java中double类型四舍五入的方法总结
    代码:doublea=13.245; //方法一:BigDecimalbd=newBigDecimal(a);Doubleb=bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();System.out.println("方法一:"+b);//方法二:DoublemyValue=newBigDecimal(a).setScale(2,BigDecimal.ROUND_HALF_UP)......
  • Java 计算数学表达式(字符串解析求值工具)
    Java字符串转换成算术表达式计算并输出结果,通过这个工具可以直接对字符串形式的算术表达式进行运算,并且使用非常简单。这个工具中包含两个类Calculator和ArithHelperCalculator代码如下:importjava.util.Collections;importjava.util.Stack;/***算数表达式求值*......
  • java单例模式几种实现方式
    1、饿汉式(线程安全,调用效率高,但是不能延时加载):publicclassImageLoader{privatestaticImageLoaderinstance=newImageLoader;privateImageLoader(){}publicstaticImageLoadergetInstance(){returninstance;}}一上来就把单例对象创建出来了,要用的时候直......
  • Java8 Lambda表达式
    学习资料:https://www.bilibili.com/video/BV1ci4y1g7qD/?spm_id_from=333.337.search-card.all.click&vd_source=46d50b5d646b50dcb2a208d3946b1598......