gpt4 book ai didi

java-native-interface - jdk1.0 为何缺乏 native 代码和 Java 代码之间的清晰分离?

转载 作者:行者123 更新时间:2023-11-30 06:30:17 25 4
gpt4 key购买 nike

来自 IBM Link :

The JDK 1.0 release included an earlier native-method interface that lacked clean separation between native and Java code. In this interface, natives would reach directly into JVM structures and so could not be portable across JVM implementations, platforms, or even versions of the JDK. Upgrading an application with a substantial number of natives using the JDK 1.0 model was expensive, as was developing natives that could run with multiple JVM implementations.

什么是本地人会达到 直接进入 JVM 结构,因此不能跨 JVM 实现移植?我不明白这一点。

最佳答案

documentation for the current JNI包含有关旧界面的有趣信息:

JDK 1.0 was shipped with a native method interface. Unfortunately, there were two major reasons that this interface was unsuitable for adoption by other Java VMs.

First, the native code accessed fields in Java objects as members of C structures. However, the Java Language Specification does not define how objects are laid out in memory. If a Java VM lays out objects differently in memory, then the programmer would have to recompile the native method libraries.

...

这意味着 native 代码可以直接访问内存中对象的值,而不是通过某些 API。这相当于直接访问 Java 中的字段,而不是使用 getter/setter。这意味着在不破坏与使用该旧 native 界面的任何代码的兼容性的情况下,永远无法更改字段的底层布局。

我找到了 copy of the native method tutorial from the JDK 1.0.2 here .它包括关于 handling Java objects from native code 的部分.

现代 JNI 仅通过方法调用提供对 Java 对象字段的访问。通过这种方式,对象的布局和许多其他内容可以在不破坏兼容性的情况下进行更改。

关于java-native-interface - jdk1.0 为何缺乏 native 代码和 Java 代码之间的清晰分离?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10612623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com