Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Who implements the OS interfacing in java?

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)

like image 347
Skeen Avatar asked Aug 26 '13 20:08

Skeen


1 Answers

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.

like image 84
Jan Dörrenhaus Avatar answered Oct 04 '22 16:10

Jan Dörrenhaus