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

Compiling and Executing the Program

I've saved the C program we wrote as invoke.c.

In order to compile the program to a .exe file, we'll need:

  • The JNI headers,
  • The import library we built earlier.

The JNI headers come with the JDK, in its /include and /include/win32 directories. We add them to the gcc command line with -I. We can also specify libraries with the -L argument, and name libraries with -l (that's lowercase L). My command line looks like this (where the jdk environment points to my Java installation):

gcc -mno-cygwin -o invoke.exe -I$jdk/include -I$jdk/include/win32 invoke.c -L. -ljvm
Recall that I said I was putting everything in one directory. So my libjvm.dll.a is sitting in my current working directory (hence -L.), and I can refer to it by name as jvm.

In order to execute the program, we must have the JVM's jvm.dll in our Windows PATH variable. So, to run our program using the "Classic" VM, my command line looks like:

PATH=$jdk/jre/bin/classic:$PATH; invoke
If you want to use HotSpot, it will look something like:
PATH=$jdk/jre/bin/hotspot:$PATH; invoke

If you have everything set up correctly, you should see:

$ PATH=$jdk/jre/bin/hotspot:$PATH; invoke
Hello, World!
Arguments sent to this program:
From-C-program
As long as you manage the PATH variable properly, you should be able to execute the Java program from inside or outside Cygwin, since we used the -mno-cygwin option on gcc.

Please let me know if you find errors or omissions in this walkthrough, or have general comments or questions.