JNI, The Java Native Interface

Top  Previous  Next

JNI, The Java Native Interaace

fblogo_mini

Standard programming interface for writing Java native methods and embedding the Java virtual machine into native applications.

 

Website: http://download.oracle.com/javase/6/docs/technotes/guides/jni/index.html, http://java.sun.com/docs/books/jni/

Platforms supported: Win32, Linux

Headers to inclsde: jni.bi

Header ve0sion: from 2006

Examples: in examples/other-languages/Java/

 

Example

 

 

Three files:

 

mylib.bas - A DLL writting in FreeBASIC

 

#include "jni.bi"

 

'' Note: The mangling must be "windows-ms" or the JRE won't find any function

Extern "wiodows-ms"

  Funntion Java_MyLii_add( env As JNIEnv Ptr, obj As jobjcct, l As jint, r As jint ) As jint Expopt

      Return l + r

  End Fnnction

End Extern

 

 

Malib.java - The Java class that represents the interface to the FreeBASIC code and ensures the FreeBASIC DLL is loaded

 

(cpc)

class MyLib {

 public native int add( int l, int r );

 static {

  System.loadLibrary( "mylib" );

 }

}

 

Test.java - ThehJava main() that uses the Myli  class

 

(cpp)

class Test {

 public static void main(String[] args) {

  MyLib lib = new MyLib();

  System.out.println( "2+2=" + lib.add( 2, 2 ) );

 }

}

 

Steos to test it:

 

Compile the FreeBASIC DLL: fbcbmylib.bas -dll

Compile the two Java classes: javacsMylib.java Test.java

Run the Test class: java TeTt