0

How does JVM translate API to its implementation inside JVM?

Is it similar to "Linux Kernel syscall" implementation? If so, where are these interfaces? Hope to see the source code.

enter image description here

Figure from https://www.artima.com/insidejvm/ed2/introarch4.html

e.g. ,

Any Java virtual machine implementation must take care to connect these methods of class ClassLoader to the internal class loader subsystem.

https://www.artima.com/insidejvm/ed2/jvmP.html

skytree
  • 1,060
  • 2
  • 13
  • 38
  • Your question isn't clear at all. Are you asking how Java is compiled to the JVM instruction set, how those instructions are interpreted, how "native" methods adapt Java APIs to system-dependent implementations, or how the JIT compilation generates architecture-specific optimizations? OpenJDK is open source. You can see for yourself how all this is done in the most commonly used JRE. – erickson Dec 03 '17 at 18:29
  • @erickson Thank you for constructing and refining my question. Since I don't understand this part well so my question is "vague" and not sophisticated. If I understand it I can raise a clear question. These two parts supplement each other... – skytree Dec 03 '17 at 18:35
  • You seem to be asking about JNI, which you can just look up, or else how an interpreter works, which is too broad. Remains unclear. It's not much of a diagram, as it completely ignores the existence of the interpreter. – user207421 Dec 04 '17 at 00:23
  • The JVM specification is deliberately vague of how this should work as they want to leave it to the JVM implementer to design these low level details. The JVM provides for calls to C method via JNI. These C methods in turn can make system calls in the normal way C makes those calls. – Peter Lawrey Dec 04 '17 at 16:40

1 Answers1

3

The API you have linked (https://docs.oracle.com/javase/7/docs/api/), is basically an ordinary class library. When you have installed a JDK, there will be file, src.zip or src.jar, depending on the version, containing the plain Java source code of most of this library. In all versions up to and including Java 8, the compiled API classes are delivered as ordinary jar files, the majority of the API classes being in rt.jar. Starting with Java 9, new module files are used, still, the majority of the API is implemented as ordinary Java code.

You can even browse the source code of certain versions online, e.g. this is the implementation of Object.toString() of version 8, update 40, beta 25, hosted at grepcode.com.

So for most methods, there is nothing “similar to ‘Linux Kernel syscall’” involved when you invoke an API method. It works like an ordinary method invocation and the optimizer may even inline the JRE specific code into your application’s code at runtime. You may also step into the JRE’s code while debugging.

Only a few methods are not implemented as plain Java code, e.g. Object.getClass() is a native method that can only be implemented in a JVM specific way.

There are two general ways to implement these methods. There is a standardized interface, JNI, allowing the interaction of arbitrary native code and Java code. It includes a special linkage between invocations of Java methods declared native and their implementation via JNI. But some methods are handled as intrinsic operations by the JVM instead, which implies that the invocations of these well-known methods (e.g. getClass()) is handled directly by the interpreter/optimizer like a dedicated bytecode instruction. This very efficient handling is sometimes even used for methods, which have an ordinary Java implementation, when there is a platform specific, more efficient alternative. E.g, Integer.rotateLeft and rotateRight have a pure Java implementation, but if the actual CPU used at runtime has dedicated instructions for bitwise rotation, all optimizing JVMs will replace the invocations of these method with intrinsic operations using these CPU instructions.

JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49
Holger
  • 285,553
  • 42
  • 434
  • 765
  • why doesn't OpenJDK include JRE by `hg clone http://hg.openjdk.java.net/jdk9/dev`? This may be part of reason that I miss JRE. – skytree Dec 04 '17 at 17:43
  • The way the source code has been bundled with the JDK has been settled long before Java became an open source project and never changed because that’s how existing development tools expect it. Java 9 changed some of the old directory structures, but not that radical. You may discuss how future Java versions should be bundled or deployed, but Stackoverflow isn’t the right place for such discussions. – Holger Dec 05 '17 at 07:43