Previous | Next | Trail Map | Integrating Native Methods into Java Programs | Table of Contents

Integrating Native Methods into Java Programs

by Mary Campione

A native method is a Java method (either an instance method or a class method) whose implementation is written in another programming language such as C. The lessons in this trail show you how to integrate native methods into your Java code.

Note: This trail reflects the native method API in the 1.0.2 release of the JDK. This trail has been superceded by Integrating Native Code and Java Programs(in the new JDK 1.1 documentation).

Only experienced programmers should attempt to use native methods!

Note to C++ programmers: Note that currently the Java environment generates C header and source files, which provide the glue for integrating C code into Java programs. You can integrate C++ code into your Java programs using these C header and source files. See Using C++ in Native Method Implementations for information (this will make more sense after you've read both lessons in this trail).

WARNING! The programming interface for implementing native methods in Java is under construction. Use these interfaces at your own risk and in full knowledge that they will change in future releases of Java. The examples and information contained here reflect the JDK 1.0 release.

Step By Step walks you step by step through a simple example (the "Hello World!" of native methods) to illustrate how to write, compile, and run a Java program with native methods.

Implementing Native Methods shows you how to implement both the Java side and the native language side of a native method. This lesson includes information about passing arguments of various data types into a native method and returning values of various data types from a native method. This lesson also describes many useful functions that your native language code can use to access Java objects and their members, create Java objects, throw exceptions, and more.

Security consideration: Note that the ability to load dynamic libraries is subject to approval by the current security manager. When working with native methods, you must load dynamic libraries. So applets may not be able to use native methods depending on the browser or viewer they are running in. See Understanding Applet Capabilities and Restrictions(in the Writing Applets trail)for information about the security restrictions placed on applets.

Note to MacOS programmers: You can't extend the Java runtime on a 68K Macintosh. Thus you can't load shared libraries and you can't use native methods.

Previous | Next | Trail Map | Integrating Native Methods into Java Programs | Table of Contents