Java中的native关键字有什么用?
在玩这个谜题(这是一个 Java 关键字问答游戏)时,我遇到了 native
关键字。
Java中的native关键字有什么用?
While playing this puzzle (It's a Java keyword trivia game), I came across the native
keyword.
What is the native keyword in Java used for?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
最小可运行示例
Main.java
Main.c
编译并运行:
输出:
在 Ubuntu 14.04 AMD64 上测试。还适用于 Oracle JDK 1.8.0_45。
GitHub 上的示例供您使用。
Java 包/文件名中的下划线必须在 C 函数名称中使用
_1
进行转义,如下所述:在包含下划线的 Android 包名称中调用 JNI 函数解释
native
允许您:这可用于:
,但可移植性较低。
您也可以从 C 调用 Java,但您必须首先用 C 创建 JVM:如何从 C++ 调用 Java 函数?
出于同样的原因,许多其他“VM 语言”中也存在类似的本机扩展 API,例如 Python, Node.js Node.js,Ruby。
Android NDK
在这种情况下,概念完全相同,只是您必须使用 Android 样板来设置它。
官方 NDK 存储库包含“规范”示例,例如 hello-jni 应用程序:
在 Android O 上使用 NDK 解压
.apk
时,您可以看到预编译的.so
对应的内容lib/arm64-v8a/libnative-lib.so
下的本机代码。TODO确认:此外,
file /data/app/com.android.appname-*/oat/arm64/base.odex
说它是一个共享库,我认为它是AOT预编译的.dex与 ART 中的 Java 文件相对应,另请参阅:Android 中的 ODEX 文件是什么? 那么也许 Java 实际上也是通过native
接口运行的?OpenJDK 8 中的示例
让我们查找 jdk8u60-b27 中定义
Object#clone
的位置。我们将得出结论,它是通过
native
调用实现的。首先我们发现:
这导致我们jdk/src/share/classes/java/lang/Object.java#l212:
现在是困难的部分,找到克隆在所有间接中的位置。对我有帮助的查询是:
它将找到可能实现 Object 的本机方法的 C 或 C++ 文件。它引导我们到 jdk/share/native/java/lang/Object.c#l47:
它引导我们到
JVM_Clone
符号:它引导我们到 热点/src/share/vm/prims/jvm .cpp#l580:
展开一堆宏后,我们得出的结论是,这就是定义点。
Minimal runnable example
Main.java
Main.c
Compile and run:
Output:
Tested on Ubuntu 14.04 AMD64. Also worked with Oracle JDK 1.8.0_45.
Example on GitHub for you to play with.
Underscores in Java package / file names must be escaped with
_1
in the C function name as mentioned at: Invoking JNI functions in Android package name containing underscoreInterpretation
native
allows you to:This could be used to:
with the tradeoff of lower portability.
It is also possible for you to call Java from C, but you must first create a JVM in C: How to call Java functions from C++?
Analogous native extension APIs are also present in many other "VM languages" for the same reasons, e.g. Python, Node.js, Ruby.
Android NDK
The concept is exact the same in this context, except that you have to use Android boilerplate to set it up.
The official NDK repository contains "canonical" examples such as the hello-jni app:
In you
unzip
an.apk
with NDK on Android O, you can see the pre-compiled.so
that corresponds to the native code underlib/arm64-v8a/libnative-lib.so
.TODO confirm: furthermore,
file /data/app/com.android.appname-*/oat/arm64/base.odex
, says it is a shared library, which I think is the AOT precompiled .dex corresponding to the Java files in ART, see also: What are ODEX files in Android? So maybe the Java is actually also run via anative
interface?Example in the OpenJDK 8
Let's find find where
Object#clone
is defined in jdk8u60-b27.We will conclude that it is implemented with a
native
call.First we find:
which leads us to jdk/src/share/classes/java/lang/Object.java#l212:
Now comes the hard part, finding where clone is amidst all the indirection. The query that helped me was:
which would find either C or C++ files that might implement Object's native methods. It leads us to jdk/share/native/java/lang/Object.c#l47:
which leads us to the
JVM_Clone
symbol:which leads us to hotspot/src/share/vm/prims/jvm.cpp#l580:
After expanding a bunch of macros, we come to the conclusion that this is the definition point.
它标志着一个方法,它将用其他语言实现,而不是用 Java 实现。它与 JNI(Java 本机接口)一起工作。
过去使用本机方法来编写性能关键部分,但随着 Java 变得越来越快,这种情况现在不太常见。 时,需要使用本机方法
您需要从 Java 调用以其他语言编写的库
您需要访问只能通过其他语言(通常是 C)访问的系统或硬件资源。实际上,许多与真实计算机交互的系统功能(例如磁盘和网络 IO)只能执行此操作,因为它们调用本机代码。
参见
Java 本机接口规范
It marks a method, that it will be implemented in other languages, not in Java. It works together with JNI (Java Native Interface).
Native methods were used in the past to write performance critical sections but with Java getting faster this is now less common. Native methods are currently needed when
You need to call a library from Java that is written in other language.
You need to access system or hardware resources that are only reachable from the other language (typically C). Actually, many system functions that interact with real computer (disk and network IO, for instance) can only do this because they call native code.
See Also
Java Native Interface Specification
native
关键字应用于方法,表示该方法是使用 JNI(Java 本机接口)在本机代码中实现的。The
native
keyword is applied to a method to indicate that the method is implemented in native code using JNI (Java Native Interface).直接来自Java 语言规范:
Straight from the Java Language Specification:
正如 SLaks 回答的那样,
native
关键字用于调用本机代码。GWT 也使用它来实现 javascript 方法。
As SLaks answered, the
native
keyword is for calling native code.It also used by GWT for implementing javascript methods.
实现本机代码的函数被声明为本机。
http://en.wikipedia.org/wiki/Java_Native_Interface
functions that implement native code are declared native.
http://en.wikipedia.org/wiki/Java_Native_Interface
NATIVE 是非访问修饰符。它只能应用于 METHOD。
它指示方法或代码的平台相关实现。
NATIVE is Non access modifier.it can be applied only to METHOD.
It indicates the PLATFORM-DEPENDENT implementation of method or code.
Java
native
方法为 Java 代码提供了一种出于功能或性能原因调用操作系统本机代码的机制。示例:
在OpenJDK中相应的
Runtime.class
文件中,位于JAVA_HOME/jmods/java.base.jmod/classes/java/lang/Runtime .class
,包含这些方法并用ACC_NATIVE
(0x0100
) 标记它们,并且这些方法不包含 Code 属性,这意味着这些方法没有任何实际编码Runtime.class
文件中的逻辑:availableProcessors
:标记为本机且无 Code 属性freeMemory
:标记为本机且无 Code方法totalMemory
:标记为本机且无代码属性maxMemory
:标记为本机且无代码属性gc
:标记为本机且无代码属性其实编码逻辑在对应的Runtime.c 文件:
而这些
C
编码被编译成libjava.so
(Linux )或libjava.dll
(Windows) 文件,位于JAVA_HOME/jmods/java.base.jmod/lib/libjava.so
:< a href="https://i.sstatic.net/HjkZT.png" rel="noreferrer">
参考
Java
native
method provides a mechanism for Java code to call OS native code, either due to functional or performance reasons.Example:
In the corresponding
Runtime.class
file in OpenJDK, located inJAVA_HOME/jmods/java.base.jmod/classes/java/lang/Runtime.class
, contains these methods and tagged them withACC_NATIVE
(0x0100
), and these methods do not contain the Code attribute, which means these method do not have any actual coding logic in theRuntime.class
file:availableProcessors
: tagged as native and no Code attributefreeMemory
: tagged as native and no Code attributetotalMemory
: tagged as native and no Code attributemaxMemory
: tagged as native and no Code attributegc
: tagged as native and no Code attributeThe in fact coding logic is in the corresponding Runtime.c file:
And these
C
coding is compiled into thelibjava.so
(Linux) orlibjava.dll
(Windows) file, located atJAVA_HOME/jmods/java.base.jmod/lib/libjava.so
:Reference
native 是 java 中的一个关键字,用于使未实现的结构(方法)像抽象一样,但它依赖于平台,例如本机代码,并从本机堆栈而不是 java 堆栈执行。
native is a keyword in java , which is used to make unimplemented structure(method) like as abstract but it would be a platform dependent such as native code and execute from native stack not java stack.
native
是Java中的一个关键字,它表示平台相关。native
方法充当 Java (JNI) 和其他编程语言之间的接口。native
is a keyword in Java, it indicates platform dependent.native
methods act as an interface between Java (JNI) and other programming languages.