External Interfaces Previous page   Next Page

The Java Class Path

MATLAB loads Java class definitions from files that are on the Java class path. The class path is a series of file and directory specifications that MATLAB uses to locate class definitions. When loading a particular Java class, MATLAB searches files and directories in the order they occur on the class path until a file is found that contains that class definition. The first definition that is found ends the search.

The Java class path consists of two segments: the static path and dynamic path. The static path is loaded at the start of each MATLAB session and cannot be changed without restarting MATLAB. The dynamic path can be loaded and modified at any time during a session using MATLAB functions. MATLAB always searches the static path before the dynamic path.

You can view these two path segments using the javaclasspath function:

You will probably want to use both the static and dynamic paths:

The Static Path

The static Java class path is loaded at the start of each MATLAB session from the file classpath.txt. The static path offers better Java class loading performance than the dynamic path. However, to modify the static path you need to edit the file classpath.txt and then restart MATLAB.

Finding and Editing classpath.txt.   The default classpath.txt file resides in the toolbox\local subdirectory of your MATLAB root directory:

To make changes in the static path that will affect all users who share this same MATLAB root, you can edit this file in toolbox\local. If you want to make changes that will not affect anyone else, copy classpath.txt to your own startup directory and edit the file there. When MATLAB starts up, it looks for classpath.txt first in your startup directory, and then in the default location. It uses the file it finds first.

To see which classpath.txt file is currently being used by your MATLAB environment, use the which function:

To edit either the default file or the copy you have made in your own directory, enter the following command in MATLAB:

Special Symbols in classpath.txt.   You can designate special tokens or macros in the classpath.txt file using a leading dollar sign, (e.g., $matlabroot or $jre_home). However, this can cause problems if you use this sign in any of your class directory paths. For example, the following path string does not correctly represent the path to a directory named hello$world:

You must use two consecutive dollar signs in classpath.txt to represent a single $ character. So, to correctly specify the directory path shown above, you need to use the following text:

The Dynamic Path

The dynamic Java class path can be loaded at any time during a MATLAB session using the javaclasspath function. You can define the dynamic path (using javaclasspath), modify the path (using javaaddpath and javarmpath), and refresh the Java class definitions for all classes on the dynamic path (using clear java) without restarting MATLAB. See the function reference pages for more information on how to use these functions.

Although the dynamic path offers more flexibility in changing the path, you may notice that Java classes that are on the dynamic path load more slowly than those on the static path.


Previous page  Bringing Java Classes and Methods into MATLAB Making Java Classes Available to MATLAB Next page

© 1994-2005 The MathWorks, Inc.