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(). 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 library 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).


Security consideration: Note that the ability to load dynamic libraries is subject to approval by the current security manager. When working with native methods, you must load dynamic libraries. So applets may not be able to use native methods depending on the browser or viewer they are running in. See Understanding Applet Capabilities and Restrictions(in the Writing Applets trail)for information about the security restrictions placed on applets.


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


Casa de Bender