-2

I know that Java give a powerful advantagte: Java code is compiled to bytecode and this bytecode is executed by JVM, hence Java is about portability. However, there exists functions such that their implementation depends on operating system.

https://speakerdeck.com/raboof/jvm-hacking (4-th slide)
As you can see there are system-depended C code. How does it work ? I mean that the same jar using bind method can be executed on windows and linux. After all, bind method on linux and windows can be fairly different (number of paramers, name and more).

Can you explain it me?

Haskell Fun
  • 299
  • 4
  • 13
  • I haven't seen the underlying JVM or JDK implementation, but [maybe this](https://stackoverflow.com/questions/5919996/how-to-detect-reliably-mac-os-x-ios-linux-windows-in-c-preprocessor) will shed some light on a possible way of doing it. – Christopher Schneider Sep 11 '17 at 19:24
  • So, simply native C code executed from JVM (runtime) has something like if WIN_32, yeah ? – Haskell Fun Sep 11 '17 at 19:31
  • I didn't say that. I said that's a possible way of doing it. Remember also that each environment has its own JRE, so while the executable JAR is the same, the JRE is not. – Christopher Schneider Sep 11 '17 at 19:34

1 Answers1

0

It's the JVM and its runtime library that do the magic.

Pure Java programs consist of bytecode, and the JVM interprets or compiles this bytecode, so the local CPU / OS can execute it.

Then there are methods declared to be "native", and the specific Java runtime library for Windows x86 has its implementation, the x64 version has another implementation, the Linux XYZ has yet another implementation and so on. The signature (number and type of parameters) of these native methods is the same over all implementations, buildung a common abstraction over the Windows/Linux/macOS functionalities that they wrap.

If you need to use native methods yourself in your code, you must supply all the necessary implementations with your program (.dll, .so or whatever your targetted systems need). But luckily, that's rarely necessary as the Java library is fairly complete...

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7