Question

My question is regarding mostly the standard, but input on how specific implementations deal with the issue is welcome too. So without further redo, my question is;

  • Who is responsible for the operating system interfacing in Java?
    • The Java Virtual Machine?
    • or the Java Class Library?

Also how is this usually implemented, through Java Native Interface?

Example; I'm reading a file, using java.io.FileReader. When invoking read on this object the JVM will obviously call this function, in the correct class file, in the JCL, however will this code in the end rely on the JVM for calling, for instance the posix read function? - or will the class file do this itself, through the use of JNI? (assuming a read has to be done, that is the file isn't already in cache/memory)

Was it helpful?

Solution

I was hoping some real professional would answer this, preferrably someone who has actually worked on a JVM/JDK. Since so far, none of them seem to be online (or have seen your question, for that matter), I will have a go at explaining this.

The Java Class Library classes (found in the rt.jar file of your JRE/JDK) are in fact pure java. They do contain a lot of JNI calls, though. If you look at the source of FileInputStream for example, you will find stuff like this:

private native int readBytes(byte b[], int off, int len)
    throws IOException;

public int read(byte b[])
    throws IOException
{
    return readBytes(b, 0, b.length);
}

So your guess was correct: the JCL does make extensive use of JNI. But who provides those native implementations? Simple: The JVM. And that is how the pieces fit together.

OTHER TIPS

To answer your core question, the JVM is responsible for the OS shell integration. The Java Class Library (API) is a set of functionality that uses the JVM, that's why it is also called an API.

Regarding the JNI integration, the JVM is handling these calls. At your class level, you have to declare the code that has a native implementation.

Let's take for instance the System.arraycopy() method. This method has a native implementation. The System class defines the signature of the method and the JVM is responsible for translating the call from the System.arraycopy() method to the native underlaying implementation. If you download the OpenJDK sources from here, you can check this .cpp file hotspot\src\share\vm\oops\objArrayKlass.cpp:

void objArrayKlass::copy_array(arrayOop s, int src_pos, arrayOop d,
                       int dst_pos, int length, TRAPS) { ... } 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top