Previous | Next | Trail Map | Writing Java Programs | Using System Resources


Loading Dynamic Libraries

System provides two methods that allow your Java applications to load dynamic libraries: load() and loadLibrary().


Note: Applets are not allowed to load dynamic libraries. A call to either load() or loadLibrary() from within an applet will result in a SecurityException.

Typically, your program will call one of these methods from within the static initializer of a class when you are working with native methods. See Integrating Native Methods into Java Programs(in the Integrating Native Methods into Java Programs trail)for information about writing native methods.

The load() method requires the complete path name of the library as an argument. For example, on a Solaris system you might write:

System.load("/home/me/libs/libmylib.so");
to load the libmylib.so library in the /home/me/libs directory.

Using the load() method is system-dependent because it uses a pathname to load the library and pathnames are usually system-dependent. Thus, loadLibrary() is sometimes a better choice. However, dynamically loadable libraries are system-dependent in nature so the use of load() may not compromise system-independence any more than the act of loading the library itself.

The loadLibrary() method requires just the name of a to load:

System.loadLibrary("mylib");
The loadLibrary() method searches for the library. The search performed by loadLibrary() depends on the system you are running on, but typically, it searches the directories listed in one of your environment variables set up to that purpose. This is covered in detail in Integrating Native Methods into Java Programs(in the Integrating Native Methods into Java Programs trail).


Previous | Next | Trail Map | Writing Java Programs | Using System Resources