Treffer: Java/C++ integration: Writing native Java methods in natural C++ Per Bothner
Weitere Informationen
“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 ma-jor problems: JNI code has major inherent inefficiencies, because evrything thas to be done as calls through a run-time 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 Com-piler Collection (Gcc) framework. GCJ offers an in ad-dition to JNI an alternative, the Compiled Native Inter-face (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 Java-aware 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 stan-dard 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. 1