JVM Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.10

Loading Native Libraries

This section provides a tutorial example on how to load native libraries (DLL files on Windows systems) with loadLibrary() and load() methods.

The JVM instance also allows you to load native libraries into the system to support native methods by using loadLibrary() and load() methods:

  • loadLibrary() - Loads a native library in the library path with the given library name.
  • load() - Loads a native library in the file system with the given full path name.

Here is tutorial example program testing loadLibrary() and load() methods:

/**
 * RuntimeLoadLibrary.java
 * Copyright (c) 2010 by Dr. Herong Yang, herongyang.com
 */
class RuntimeLoadLibrary {
   public static void main(String[] a) {
      java.io.PrintStream out = System.out;
      Runtime rt = Runtime.getRuntime();
     
      try {
         out.println("Loading a native library...");
         rt.loadLibrary("msvcr71"); // c:/local/jdk/bin/msvcr71.dll

         out.println("Loading a native code...");
         rt.load("c:/local/jdk/bin/java.exe");
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

When executed on my Windows XP system with JDK 1.6.0, I got this result:

C:\herong\jvm>java RuntimeLoadLibrary
Loading a native library...
Loading a native code...

The test result tells me that:

  • Native libraries are DLL files on Windows systems.
  • load() can also loads EXE files on Windows systems.

I don't have any example programs for calling native methods provided in native libraries yet.

Last update: 2010.

Table of Contents

 About This Book

 Download and Install Java SE 1.6 Update 2

java.lang.Runtime Class - The JVM Instance

 What Is Runtime?

 Printing Runtime Basic Information

 Running the Garbage Collector Explicitly

 Shutting Down or Terminating the JVM Instance

 Executing System Commands

Loading Native Libraries

 java.lang.System Class - The Operating System

 ClassLoader Class - Class Loaders

 Class Class - Class Reflections

 Sun's JVM - Java HotSpot VM

 JRockit JVM 7.0 by BEA Systems

 JRockit JVM 8.0 by BEA Systems

 Memory Management Rules and Tests

 Garbage Collection Tests

 Stack Overflow Tests

 Thread Testing Program and Result

 StringBuffer Testing Program and Result

 CDS (Class Data Sharing)

 Micro Benchmark Runner and JVM Options

 Micro Benchmark Tests on "int" Operations

 Micro Benchmark Tests on "long" Operations

 Micro Benchmark Tests in JIT Compilation Mode

 Micro Benchmark Tests on "float" and "double" Operations

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2010
Loading Native Libraries