HP-UX Programmer's Guide for Java 2

Table Of Contents
The Invocation API is provided by the C/C++ header file jni.h. It actually provides
two different interfaces. It provides a standard C interface and an object based C++
interface.
Here is the sample Java™ class that we will call from C and C++ programs:
//
// File TestNonJavaCallingJava2.java
//
public class TestNonJavaCallingJava2 {
public static void printInt(int arg)
{
System.out.println("TestNonJavaCallingJava2.\
printInt received: " + arg);
}
}
Compile the above Java™ file into byte code:
$ <java_dir>/bin/javac -verbose TestNonJavaCallingJava2.java
Output:
TestNonCallingJava2.class
HP provides the following two examples on how to call Java™ from C and C++ using
the standard JNI calling mechanism.
Sample code for SDK 1.3.1
Sample native calling Java™ implementation in C
Here is the sample C program. This program will use the JVM Invocation Interface to
create a new JVM, find a class named TestNonJavaCallingJava2, and find a Java™
method named printInt, and invoke the method with an argument of 100.
NOTE: This program works as is for SDK 1.4.2. However, if you wish to take advantage
of the new JNI features in 1.4.2, in the example below you should change the line
vm_args.version = JNI_VERSION_1_2; to vm_args.version =
JNI_VERSION_1_4;
/*
* File: c_main.c
*
* Example C Source File as the Main
* create a new Java Virtual Machine
* and locate the class TestNonJavaCallingJava2
* and invoke the static method printInt
*/
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
Native (non-Java) calling Java™ methods 53