5

I have spent some time learning what are Java Native methods and that they are implemented in platform dependent code(mostly C).

But where can I find those native implementations of Java? eg : sleep(long millis) method of Thread class is native. But where is its implementation code???

killerCoder
  • 407
  • 2
  • 7
  • 14
  • 1
    Maybe here? http://stackoverflow.com/questions/2292629/where-to-find-source-code-for-java-lang-native-methods – zw324 Jul 02 '11 at 13:43

5 Answers5

4

But where can I find those native implementations of Java?

You'll have to download the complete source of some JDK. In OpenJDK for instance, you'll find lots of files related to threads:

./jdk/src/share/native/java/lang/Thread.c
aioobe
  • 399,198
  • 105
  • 792
  • 807
4

The native code is implemented within the JVM (Java Virtual Machine). The Java developer isn't supposed to worry about their implementation as they relate to the inner working of the virtual machine. However, you can write your own native methods using JNI or see how they are implemented for a specific JVM.

Jihed Amine
  • 2,119
  • 19
  • 32
1

You can find the implementations of these functions in the source code of your JRE. E.g. the source code of OpenJDK 6 is here.

thkala
  • 80,583
  • 22
  • 150
  • 196
1

Each JVM has its own implementation. You may find the implementation of OpenJDK here : http://openjdk.java.net/

JB Nizet
  • 657,433
  • 87
  • 1,179
  • 1,226
0

The implementation of these code is in the source code of the OpenJDK, located at github.com/openjdk/jdk .

Example for java.lang.Runtime:

Native code for the garbage collection method System.gc():

JNIEXPORT void JNICALL
Java_java_lang_Runtime_gc(JNIEnv *env, jobject this)
{
    JVM_GC();
}
JVM_ENTRY_NO_ENV(void, JVM_GC(void))
  JVMWrapper("JVM_GC");
  if (!DisableExplicitGC) {
    Universe::heap()->collect(GCCause::_java_lang_system_gc);
  }
JVM_END

Reference

Happy
  • 606
  • 8
  • 14