Ros*_*oto 4 java java-native-interface jvm
我和我的老板发生了争执,他确信 JVM 使用 JNI 来访问诸如文件系统之类的本机事物。我认为他错了,因为 JVM 本身是本机代码,它直接与操作系统通信——它不需要 JNI 样板来访问文件系统。
请帮我澄清JVM是如何工作的
小智 5
这确实有点有争议。Java 本机接口是一种语言功能,允许您在 Java 中定义一个函数调用,该函数调用将传递给非 Java 代码,特别是平台本机的代码。如果您查看 SDK 的 src.zip 中的 FileOutputStream.java,您将看到如下代码:
    /**
 * Opens a file, with the specified name, for writing.
 * @param name name of file to be opened
 */
private native void open(String name) throws FileNotFoundException;
/**
 * Opens a file, with the specified name, for appending.
 * @param name name of file to be opened
 */
private native void openAppend(String name) throws FileNotFoundException;
/**
 * Writes the specified byte to this file output stream. Implements 
 * the <code>write</code> method of <code>OutputStream</code>.
 *
 * @param      b   the byte to be written.
 * @exception  IOException  if an I/O error occurs.
 */
public native void write(int b) throws IOException;
所以我会说如果问题是 - 类库是否使用与我访问外部系统级库调用相同的符号,我认为答案是肯定的。
但是,解释 Java 字节码并应用这些规则的 Java 虚拟机绝对是本机代码 - 我还怀疑为了命名(不同的“本机”系统使用完全不同的 API)与直接对库的本机调用不同,这些调用被选择由 VM 启动并由 VM 处理。