英文:
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
-
jdk7u存储库已过时。它已经超过8年没有更新了。最新的存储库位于https://hg.openjdk.java.net/jdk/jdk/file。
-
FileOutputStream方法的本机实现在此处。 -
如路径所示,此本机代码是libjava(
libjava.so或java.dll)的一部分。 -
libjava不是JVM,但它仍然是Java类库的重要部分,因为它包含基本类的本机方法,比如java.lang.Class,java.lang.ClassLoader等。这就是为什么JVM在引导过程中预加载libjava,参见ClassLoader::initialize。 -
只要JVM无条件地预加载
libjava,就不需要调用System.loadLibrary。
英文:
-
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
-
The native implementation of
FileOutputStreammethods is here. -
As the path suggests, this native code is a part of libjava (
libjava.soorjava.dll). -
libjavais not the JVM, but it is still the essential part of Java Class Library, since it contains the native methods for basic classes likejava.lang.Class,java.lang.ClassLoaderetc. That's why JVM preloadslibjavaduring bootstrap, seeClassLoader::initialize. -
As long as
libjavais unconditionally preloaded by the JVM, there is no need to callSystem.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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论