The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Trail: Java Native Interface
Lesson: Writing a Java Program with Native Methods

## Creating and Loading Shared Libraries

> Procedures for creating and loading shared libraries are platform dependent. This page provides some answers regarding naming conventions for shared libraries and locating shared libraries.

### Naming Conventions

Each Java runtime environment provides a platform-dependent mechanism for mapping between the actual file name of a shared library and the name string that you pass as an argument to the `System.loadLibrary` method.

- Solaris:

  The shared library file name requires a "lib" prefix and a ".so" extension. Do not include the "lib" prefix or the ".so" extension for the argument that you pass to the `System.loadLibrary method`.

- Win32:

  The shared library file name requires a ".dll" extension. Do not include the ".dll" extension for the `System.loadLibrary` method argument.

- MacOS:

  No special conventions.

### Search Mechanisms

Each Java runtime environment provides its own mechanism that indicates where to find shared libraries for native method implementations. The platform-dependent wrapper script or application shell can use the appropriate mechanism to indicate where shared libraries are located.

- Solaris:

  The `LD_LIBRARY_PATH` environment variable defines a list of directories that the Solaris VM searches for shared libraries.

- Win32:

  The Win32 VM uses a search path that includes the current directory for the process or one of the directories listed in the `PATH` environment variable.

- MacOS:

  The Code Fragment Manager provides the search mechanism used by the MRJ VM to find and load a shared library. This search path includes the application folder that contains the Macintosh Java application shell generated by `JBindery`.

### Avoiding Name Conflicts

To avoid file name conflicts, it is best to prepend directories or folders containing shared libraries to search paths, such as the `LD_LIBRARY_PATH` or `PATH` environment variables.

### Supporting Multiple Platforms

Because a Java application with native methods may support multiple platforms, it helps to organize multiple shared libraries into sub-directories by platform, such as `<app-dir>/lib/<platform>`.

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form