title: Adding a native method to OpenJDK source code
body: <p>I am trying add a custom native method(free) to OpenJDK source, so I can call that method as System.free() from inside the user Application.</p>
  
  <p>I referred online resources to do so and did following changes :</p>
  
  <pre><code>{JDK13ROOT}/src/java.base/share/classes/java/lang/Runtime.java
  public static native void free();
  
  {JDK13ROOT}/src/java.base/share/classes/java/lang/System.java
  public static void free() {
          Runtime.getRuntime().free();
  }
  
  {JDK13ROOT}/src/java.base/share/native/libjava/Runtime.c
  JNIEXPORT void JNICALL
  Java_java_lang_Runtime_free(){
       printf("Caught !\n");
  }
  </code></pre>
  
  <p>After above changes, I was able to successfully compile the Driver program which directly calls System.free(), and prints <strong>Caught !</strong> after execution.</p>
  
  <p>I want to call JVM_Free() inside Java_java_lang_Runtime_free() as below:</p>
  
  <pre><code>JNIEXPORT void JNICALL
  Java_java_lang_Runtime_free(){
       printf("Caught !\n");
       JVM_Free();
  }
  </code></pre>
  
  <p>I have included the declaration in
  <strong>{JDK13ROOT}/src/hotspot/share/include/jvm.h</strong></p>
  
  <pre><code>JNIEXPORT void JNICALL
  JVM_Free(void);
  </code></pre>
  
  <p>And the definition of same in <strong>{JDK13ROOT}/src/hotspot/share/prims/jvm.cpp</strong></p>
  
  <pre><code>JVM_ENTRY_NO_ENV(void, JVM_Free(void))
    JVMWrapper("JVM_Free");
    printf("SUCCESS\n");
  JVM_END
  </code></pre>
  
  <p>I took reference of implementation of GC() and did the same changes., but when I build the program I get the following error.</p>
  
  <pre><code>Updating support/modules_libs/java.base/libjava.so due to 1 file(s)
  /usr/bin/ld: {JDK13ROOT}/build/linux-x86_64-server-slowdebug/support/native/java.base/libjava/Runtime.o: in function `Java_java_lang_Runtime_free':
  {JDK13ROOT}src/java.base/share/native/libjava/Runtime.c:71: undefined reference to `JVM_Free'
  </code></pre>
  
  <p>I'm not able to figure out why am I not able to call any functions from Runtime.c.</p>
  
  <p>PS: I also tried adding the native method from System.c instead of Runtime.c and I see the same build failure. What am I missing or doing wrong here ?.</p>