调用 System.loadLibrary() 以加载 FileOutputStream 方法。

huangapple go评论77阅读模式
英文:

Call to System.loadLibrary() for FileOutputStream methods

问题

我正在查看Java中的FileOutputStream类的源代码,我注意到其中有一些本地方法。根据我对本地方法的理解,应该在某个地方调用了System.loadLibrary(),但是我在JDK源代码中找不到它。我想找到包含本地方法实现的动态链接库,并查看实际的System.loadLibrary()调用。有人能帮忙吗?

编辑:

为了重新表达我的问题,我想找出JDK如何在没有使用loadLibrary的情况下加载本地代码,并且真正看到在存储库中配置了什么地方。

源代码链接:https://github.com/openjdk-mirror/jdk7u-jdk

英文:

I was checking out the source code for FileOutputStream class in Java and I noticed some native methods. To my understanding of native methods there should be a call to System.loadLibrary() somewhere, but I can not find it anywhere in the JDK source. I would like to find the dynamic library which contains the implementation of the native methods and also see the actual call to System.loadLibrary(). Can anyone help?

EDIT:

To rephrase my question, I would like to find out how JDK loads native code without loadLibrary and to actually see where is that configured in the repository.

Link to source: https://github.com/openjdk-mirror/jdk7u-jdk

答案1

得分: 1

  1. jdk7u存储库已过时。它已经超过8年没有更新了。最新的存储库位于https://hg.openjdk.java.net/jdk/jdk/file。

  2. FileOutputStream 方法的本机实现在此处

  3. 如路径所示,此本机代码是libjava(libjava.sojava.dll)的一部分。

  4. libjava 不是JVM,但它仍然是Java类库的重要部分,因为它包含基本类的本机方法,比如 java.lang.Classjava.lang.ClassLoader 等。这就是为什么JVM在引导过程中预加载 libjava,参见 ClassLoader::initialize

  5. 只要JVM无条件地预加载 libjava,就不需要调用 System.loadLibrary

英文:
  1. jdk7u repository is obsolete. It hasn't been updated for more than 8 years. The up-to-date repository is at https://hg.openjdk.java.net/jdk/jdk/file

  2. The native implementation of FileOutputStream methods is here.

  3. As the path suggests, this native code is a part of libjava (libjava.so or java.dll).

  4. libjava is not the JVM, but it is still the essential part of Java Class Library, since it contains the native methods for basic classes like java.lang.Class, java.lang.ClassLoader etc. That's why JVM preloads libjava during bootstrap, see ClassLoader::initialize.

  5. As long as libjava is unconditionally preloaded by the JVM, there is no need to call System.loadLibrary.

答案2

得分: 0

FileOutputStream中的本地方法的实现包含在JVM中。System.loadLibrary()与需要单独加载的外部库一起使用,当本地方法已编译到JVM中时,就不需要它。

您可以通过在C语言中搜索“FileOutputStream”的源代码提及来查看源代码。

这个实现是特定于平台的:例如,对于Windows,您会找到调用FileOutputStream_md.c中的函数的内容,而该文件又调用io_util_md.c中的函数。

英文:

The implementations for the native methods in FileOutputStream are included in the JVM. System.loadLibrary() is used with external libraries that need to be loaded separately - it's not needed when the native methods are already compiled into the JVM.

You can see the source code by searching the source code for mentions of "FileOutputStream" in C language.

The implementation is platform specific: for example, for Windows you'll find FileOutputStream_md.c which calls functions in io_util_md.c.

huangapple
  • 本文由 发表于 2020年5月5日 20:47:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/61613540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定