The Java Native Keyword and Methods – Java本地关键字和方法

最后修改: 2019年 1月 1日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’ll discuss the concept of the native keyword in Java, and we’ll also show how to integrate native methods into Java code.

在这个快速教程中,我们将讨论Java中native关键字的概念,我们还将展示如何将native方法整合到Java代码中。

2. The native Keyword in Java

2.Java中的native关键字

First of all, let’s discuss what is a native keyword in Java.

首先,让我们讨论一下什么是Java中的 native关键字。

Simply put, this is a non-access modifier that is used to access methods implemented in a language other than Java like C/C++.

简单地说,这是一个非访问修饰符,用于访问用C/C++等Java以外的语言实现的方法

It indicates platform-dependent implementation of a method or code and also acts as an interface between JNI and other programming languages.

它表示方法或代码的平台依赖性实现,也是JNI和其他编程语言之间的接口。

3. native Methods

3.本土方法

A native method is a Java method (either an instance method or a class method) whose implementation is also written in another programming language such as C/C++.

一个原生方法是一个Java方法(无论是实例方法还是类方法),其实现也是用另一种编程语言(如C/C++)编写的。

Moreover, a method marked as native cannot have a body and should end with a semicolon:

此外,标记为native的方法不能有主体,应该以分号结束。

[ public | protected | private] native [return_type] method ();

We can use them to:

我们可以利用它们来。

  • implement an interface with system calls or libraries written in other programming languages
  • access system or hardware resources that are only reachable from the other language
  • integrate already existing legacy code written in C/C++ into a Java application
  • call a compiled dynamically loaded library with arbitrary code from Java

4. Examples

4.实例

Let’s now demonstrate how to integrate these methods into our Java code.

现在我们来演示一下如何将这些方法整合到我们的Java代码中。

4.1. Accessing Native Code in Java

4.1.在Java中访问本地代码

First of all, let’s create a class DateTimeUtils that needs to access a platform-dependent native method named getSystemTime:

首先,让我们创建一个DateTimeUtils类,该类需要访问一个名为getSystemTime的依赖平台的native方法。

public class DateTimeUtils {
    public native String getSystemTime();
    // ...
}

To load it, we’ll use the System.loadLibrary.

为了加载它,我们将使用System.loadLibrary.

Let’s place the call to load this library in a static block so that it is available in our class:

让我们把加载这个库的调用放在一个static 块中,这样它就可以在我们的类中使用。

public class DateTimeUtils {
    public native String getSystemTime();

    static {
        System.loadLibrary("nativedatetimeutils");
    }
}

We have created a dynamic-link library, nativedatetimeutils, that implements getSystemTime in C++ using detailed instructions covered in our guide to JNI article.

我们创建了一个动态链接库nativedatetimeutils,它在C++中实现了getSystemTime,其详细说明见JNI指南文章

4.2. Testing native Methods

4.2.测试native方法

Finally, let’s see how we can test native methods defined in the DateTimeUtils class:

最后,让我们看看如何测试DateTimeUtils类中定义的本地方法。

public class DateTimeUtilsManualTest {

   @BeforeClass
    public static void setUpClass() {
        // .. load other dependent libraries  
        System.loadLibrary("nativedatetimeutils");
    }

    @Test
    public void givenNativeLibsLoaded_thenNativeMethodIsAccessible() {
        DateTimeUtils dateTimeUtils = new DateTimeUtils();
        LOG.info("System time is : " + dateTimeUtils.getSystemTime());
        assertNotNull(dateTimeUtils.getSystemTime());
    }
}

Below is the output of the logger:

下面是记录器的输出。

[main] INFO  c.b.n.DateTimeUtilsManualTest - System time is : Wed Dec 19 11:34:02 2018

As we can see, with the help of the native keyword, we’re successfully able to access a platform-dependent implementation written in another language (in our case C++).

正如我们所看到的,在native关键字的帮助下,我们成功地访问了用另一种语言(在我们的例子中是C++)编写的依赖于平台的实现。

5. Conclusion

5.总结

In this article, we’ve learned the basics of native keywords and methods. With a quick example, we’ve also learned how to integrate them in Java.

在这篇文章中,我们已经了解了native关键字和方法的基本知识。通过一个简单的例子,我们还学习了如何在Java中整合它们。

The code snippets used in this article are available over Github.

本文中使用的代码片段是可通过Github获得的。