Escape the sandbox: Access native methods from an applet

Find out how you can directly invoke the Win32 API -- the IE-free way

1 2 Page 2
Page 2 of 2

Deploy the applet

Now that we've created our native DLL, our JNI Java wrapper class, our signed jar file, and our HTML file, we need to deploy them on a Web site. Just install all the files in your document base (you can, of course, create a directory for the applet code and specify a codebase).

I recommend that you create a simple batch file to automate the whole applet-creation process. I've included an example below.

This batch file compiles the Java source files, creates the signed jar archive, and copies all the files required to deploy the applet to a directory called deploy\. (See Resources to download this file.)

Click here to see the applet in action.

I've tested this applet with Communicator 4.04 with the JDK 1.1 patch and with the AWT 1.1.5 preview version of Communicator 4.05.


The functionality implemented by this applet may seem very simple, but the approach can easily be extended to access any methods available in the Win32 API -- or any native API on the client system.

While this technique provides tremendous power, there are drawbacks, as follows:

  • The development process becomes more complex
  • Two files must be copied down to the client machine (at least temporarily) to run the applet
  • Earlier Netscape versions don't support this functionality
  • Applet portability is reduced
  • Users must trust the object signer and explicitly grant privileges

Nonetheless, this approach offers a powerful alternative to an all-Microsoft solution for accessing native functionality from within an applet client.

Steve Small is a consultant for Cambridge Technology Partners. Prior to that he was with a group at Boeing responsible for developing reusable Java components for companywide use. He previously taught C and C++ for and currently teaches an Enterprise Java course at Boeing, and Java courses for the University of Washington Continuing Education Program. Steve have a BS in electrical engineering from Washington State University and a Master in software engineering from Seattle University. Steve is a Sun Certified Java Programmer.

Learn more about this topic

  • Complete source code listing of UserInfoApplet, plus an example batch file to build the applet and create a signed jar
  • Sun's Java Native Interface Specification, tutorial, and FAQ provide a very good overview of native methods
  • See Netscape's list of documentation related to object signing including tutorials, resources, tools, and FAQs
  • Read Rinaldo Di Giorgio's "Use native methods to expand the Java environment" (JavaWorld, July 1997) for a good introduction to using native methods from within Java code using JNI
  • Bret Sommers's "Outside the Sandbox" (Java Report Online, February 1998) provides an excellent tutorial describing signed applet creation
  • Java Tip 19"Java makes it easy to copy files from a Web site" (JavaWorld, December 1996) describes a clever utility for copying files from a URL
  • VeriSign offers Netscape Class 2 and Class 3 SPCs
  • Thawte Certification also offers Class 3 SPCs
  • For a very complete treatment of the Java Native Interface, see Essential JNI by Rob Gordon (Prentice Hall, ISBN0-13-679895-0)
1 2 Page 2
Page 2 of 2