InOnIt.com
Cygwin/Java
FAQ
Calling C from Java
Starting a JVM from C
Overview
Building an import library
Creating a JVM from C
Invoking the class's main() method
Compiling and executing
MLS Playoff Odds
World Football Rankings & World Cup Odds
NCAA Basketball Ratings
Hearts
Personal Home Pages
David's Company

Using The Invocation API to Create a JVM

by David Caldwell
12 February 2002
Revised 26 December 2002

In this example, we will wrap a Java application inside a Windows executable. We will write a C program to launch the Java virtual machine and start our Java application. The application we'll run will be a simple Hello World-style application:


package example.jni;

public class InvocationHelloWorld {
	public static void main(String[] args) {
		System.out.println("Hello, World!");
		System.out.println("Arguments sent to this program:");
		if (args.length == 0) {
			System.out.println("(None)");
		} else {
			for (int i=0; i<args.length; i++) {
				System.out.print(args[i] + " ");
			}
			System.out.println();
		}
	}
}

These examples were built and tested with JDK 1.4. As far as I know, any JVM 1.2 or later will work just fine. JDK 1.1 had a different (more rigid) Invocation API which is reasonably well-covered in the Java Tutorial's JNI trail, in the page on the Invocation API. Some of the information in Sun's tutorial is outdated.

This article presupposes that you understand how Windows loads DLLs, what compiling and linking are, and that you understand the basics of both the C and Java programming languages.

In this article, we put all the files in a single directory, to keep things simple. If you have other conventions for organizing projects, you can adjust the scripts and commands given accordingly.

For information on using JNI in the reverse direction -- that is, calling C functions from Java -- see the article Using Windows JNI.

References:

  • Windows API: from Microsoft. Microsoft has an amazing ability to break links; if this link does not work, you might try browsing from MSDN. You can also download something called the Platform SDK, which probably contains the API documentation. In particular, the section on DLLs may be useful.
  • Windows Dynamic-Link Libraries: Also from Microsoft's site (and therefore also in constant danger of becoming a broken link), and useful if you don't have a good background in .DLLs and how they work.
  • Java Native Interface Specification: From Sun; provides a comprehensive overview of the older JNI features. There are some new features that are described in the JNI Enhancements in JDK 1.2 and JNI Enhancements in JDK 1.4 documents.
  • Java Native Interface Tips: From Sun's web site; contains a couple of FAQ files and some links that may be useful if you want to do serious JNI (like how to write for MacOS, for instance).
  • GNU Win32 related projects (Mumit Khan): This page contains links to documentation about how to develop JNI with GNU tools on Windows, primarily relating to the MinGW project. (I should point out that it is MinGW which makes Cygwin's -mno-cygwin flag -- upon which these tutorials depend -- work.)
  • SWIG: SWIG (Simplified Wrapper and Interface Generator) is (according to its website): "a software development tool that connects programs written in C, C++, and Objective-C with a variety of high-level programming languages." According to SWIG's Java maintainer (also a Cygwin user), SWIG "generates the JNI and Java code for calling C and C++ code from Java (as well as most scripting languages). It creates Java proxy classes given a C/C++ header file." Finding the Java-specific documentation on SWIG's web site is a little tricky. Although I have not used SWIG, it looks like it would be very useful for projects which use heavy amounts of JNI. It appears that SWIG is biased toward the object-oriented development paradigm (which I consider to be a Good Thing). For projects with smaller Java-native integration needs, it appears SWIG might be overkill. Cool tech, though. Questions on SWIG can be directed to the SWIG mailing list, at swig@cs.uchicago.edu.