Archives
 
 
 
  Special
 
 
 
  About Us
 
 
 

Newsletter
Free E-mail Newsletter from BYTE.com

 
    
           
Visit the home page Browse the four-year online archive Download platform-neutral CPU/FPU benchmarks Find information for advertisers, authors, vendors, subscribers Request free information on products written about or advertised in BYTE Submit a press release, or scan recent announcements Talk with BYTE's staff and readers about products and technologies

ArticlesGoing Native


April 1997 / Javatalk / SuperCede Lives Up to Its Name / Going Native

A native method is Java's mechanism for calling on C/C++ routines. Declare a method as native, and at execution time, the Java application or applet will know that the method is separately compiled C/C++ code.

The Java native interface (JNI) defined by the Java Developer's Kit 1.1, although an improvement over the interface definition of the previous JDK, is intricate enough to require serious study before you can easily use it. The need for its complexity is u nderstandable: The native interface the JDK defines has to be implementable on virtual machines (VMs) across all Java platforms.

On the Java side, to call a native method, you must first declare it native . Next, your program calls the System.loadLibrary() call to actually load the native-code library. (You will have compiled and created the library from your C/C++ compiler.)

That's the easy part. The tough part is on the C/C++ side. Given that a method of the same name cannot exist only across classes, but also within classes, Java requires a mechanism for uniquely identifying all methods. The C/C++-generated native -method name must satisfy that requirement. You do this through mangling , a way of encoding the prototype of the method in its name. For example, suppose you have a Java class that calls a native method as follows:

package apkg;
class acls {native int ameth(int i, double j);
{ ... }

However, the C/C++ definition of the above method is:

jint Java_apkg_acls_ameth__ID (JNIEnv *env, jobject jobj, jint i, jdouble j);

If you know the formula for mangling, you can peel the name apart to determine its Java alias. The jint data type specifies this as a method returning a Java integer. Java_apkg_acls_ameth identifies this as a native Java method called ameth in package apkg , class acls . The double underscore precedes that part of the name that identifies the method's arguments. So, the I specifies the first argument as being a Java integer; the D specifies the second argument as being a Java double.

Notice that the C function's argument list includes two extra arguments: *env and jobj . The first argument is what the JNI refers to as the interface pointer . It's a pointer to a set of pointers to functions in the VM. It is through the interface pointer that the C/C++ code can call into the VM and access Java classes, objects, and data. The jobj argument is a reference to the current object ( this ).

By contrast, SuperCede's native interface is the quintessential no-brainer. On the Java side, you merely have to declare the method as native . On the C/C++ side, you identify the method as you would were it a member of a C++ class. So, for the example above, the C++ code would be:

__java_int acls::ameth(__java_int i, __java_double j)
{ ... }

You fill the method's body with code, plop the file into the SuperCede integrated development environment (IDE), and compile. That's all there is to it. SuperCede takes care of the linking.


Up to the Javatalk section contentsGo to previous article: Going NativeSearchSend a comment on this articleSubscribe to BYTE or BYTE on CD-ROM  
Flexible C++
Matthew Wilson
My approach to software engineering is far more pragmatic than it is theoretical--and no language better exemplifies this than C++.

more...

BYTE Digest

BYTE Digest editors every month analyze and evaluate the best articles from Information Week, EE Times, Dr. Dobb's Journal, Network Computing, Sys Admin, and dozens of other CMP publications—bringing you critical news and information about wireless communication, computer security, software development, embedded systems, and more!

Find out more

BYTE.com Store

BYTE CD-ROM
NOW, on one CD-ROM, you can instantly access more than 8 years of BYTE.
 
The Best of BYTE Volume 1: Programming Languages
The Best of BYTE
Volume 1: Programming Languages
In this issue of Best of BYTE, we bring together some of the leading programming language designers and implementors...

Copyright © 2005 CMP Media LLC, Privacy Policy, Your California Privacy rights, Terms of Service
Site comments: webmaster@byte.com
SDMG Web Sites: BYTE.com, C/C++ Users Journal, Dr. Dobb's Journal, MSDN Magazine, New Architect, SD Expo, SD Magazine, Sys Admin, The Perl Journal, UnixReview.com, Windows Developer Network