JNI FAQ for JDK 1.1
(Jan 1997)

Where can I download a PDF or Postscript version of the JNI specification?

PDF and Postscript versions of the JNI specification are available at the PDF and PS Documentation Page.

Why does my JNI code written for JDK 1.1 Beta stop working with the JDK 1.1 final release?

There are a few minor API changes between JDK 1.1 Beta3 and JDK 1.1 final release. Please check the CHANGES file which contains a list of JNI-related changes.

After I got an exception in native code, I tried to use FindClass to locate an exception class, so that I could raise anther exception instead of the one already raised. FindClass failed by returning 0, however. What's wrong?

Make sure that you first clear the pending exception before calling FindClass. In general, you must clear the pending exception before you make other JNI function calls. The only JNI functions that are safe to call with a pending exception are ExceptionOccurred, ExceptionDescribe and ExceptionClear.

Why does JNI_CreateJavaVM fail when I try to use the Invocation API to embed the Java VM into a native application?

A common mistake is not to specify the class path in the JDK1_1InitArgs structure, or specify the wrong class path. Make sure that the class path includes the class zip file or the directory that contains all system classes. For example, the following class path on Win32 includes the directories for your own class files (in c:\mydir\classes) as well as system classes:

  /* vmArgs is a JDK1_1InitArgs structure. */ 
  vmArgs.classpath = "c:\\mydir\\classes;c:\\jdk1.1\\lib\\classes.zip";
On Solaris, the class path separator is ":" instead of ";".

You can get an idea of what is going wrong from the error messages printed out by the VM. In a console-mode application, the VM prints the messages to stderr. In a Win32 GUI application, you can provide a "vfprintf" hook in the JDK1_1InitArgs structure to capture all VM messages. For example, you can define a function that displays messages in a window and redirect all VM messages to that window:

jint JNICALL my_vfprintf(FILE *fp, const char *format, va_list args)
{
    char buf[1024];
    _vsnprintf(buf, sizeof(buf), format, args);
    ... /* print buf in a window. */
}
...
vmArgs.vfprintf = my_vfprintf;

Note that System.err and System.out are not redirected by the vfprintf hook. You may call the appropriate Java methods in the System class to redirect these two Java streams.

I'm having trouble embedding the Java VM into a Solaris native application. What's wrong?
The Solaris Java VM shipped with JDK 1.1 is not suitable for embedding into certain native applications. Because it depends on a user-level thread package to implement Java threads, the VM overrides a number of system calls in order to support non-blocking I/O. This may have undesirable affects on the hosting native application. In addition, the Invocation API function AttachCurrentThread is not supported on Solaris.

We plan to fix these problems in the near future by releasing a Java VM directly supported by Solaris native threads.



*As used on this web site, the terms "Java virtual machine" or "JVM" mean a virtual machine for the Java platform.

Comments? Email jni@java.sun.com.
Last modified: Tue Mar 23 14:02:03 PST 1999