我和我的老板争论过,他确信JVM使用JNI来访问本地的东西,比如文件系统。我认为他错了,因为JVM本身是本机代码,它直接与操作系统通信-它不需要JNI样板来访问文件系统。
请帮我解释一下JVM的工作原理。
发布于 2010-02-03 03:48:39
这真的是一个有点无意义的观点。Java Native Interfaces是一种语言特性,允许您在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处理。
发布于 2010-02-03 03:40:46
JNI用于Java代码访问本机代码。您是对的,JVM是本机代码,因此它直接绑定到编译所针对的平台。这就是为什么每个操作系统都有一个JVM的原因。Windows JVM是为Windows、Linux for Linux、OSX for OSX等编译的。它们将所有特定于平台的代码放入JVM代码本身。
https://stackoverflow.com/questions/2187047
复制相似问题