O'Reilly logo

Java Cookbook by Ian F. Darwin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Calling Java from Native Code

Problem

You need to go the other way, calling Java from C/C++ code.

Solution

Use JNI again.

Discussion

In 1.1, JNI provides an interface for calling Java from C, with calls to:

  1. Create a JVM

  2. Load a class

  3. Find and call a method from that class (i.e., main)

This lets you add Java to legacy code. That can be useful for a variety of purposes, but entails treating Java code as an extension language (just define or find an interface like Applet or Servlet, and let your customers subclass from it).

This is not discussed in detail here, but there’s a full code example in the code archive in directory src/native1.1.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required