Java/C++ integration: Writing native Java methods in natural C++

Per Bothner, Tom Tromey · 2000

“Native methods” in Java are methods written in some other language, usually C or C++. Sun included in JDK 1.1 the “Java Native Interface” (JNI) for writing such native methods in a portable way, independent of JVM implementation. JNI satisfies that goal, but has two major problems: JNI code has major inherent inefficiencies, because evrything thas to be done as calls through a runtime table of function pointers. Using JNI is also very verbose, tedious and error-prone for the programmer. The Gnu Compiler for the Java platform (GCJ) is based on compiling Java to machine code using the Gnu Compiler Collection (Gcc) framework. GCJ offers an in addition to JNI an alternative, the Compiled Native Interface (CNI). CNI is based on the idea of making the C++ and Java data representations and calling conventions as close as practical, and using a slightly modified Javaaware C++ compiler to compile native method written in C++ methods. CNI code is both very efficient and is also very easy and natural to write, because it uses standard C++ syntax and idioms to work with Java data. The runtime and class library associated with GCJ is libgcj, which is written in a mix of Java and C++ code using CNI.

Read the paper · More papers on PaperTik