Main Content

Static Path of Java Class Path

The static path is loaded at the start of each MATLAB® session from the MATLAB built-in Java® path and the javaclasspath.txt file. The static path offers better Java class-loading performance than the dynamic Java path. However, if you modify the static path, you must restart MATLAB.

To add files to the static Java class path, create a javaclasspath.txt file. For instructions, see Create javaclasspath.txt File.

For convenience when developing your own Java classes, add entries to the dynamic Java class path. For information, see Dynamic Path of Java Class Path.

For more information about how MATLAB uses the class path, see Java Class Path.

Create javaclasspath.txt File

Each line in the javaclasspath.txt file contains a reference to a Java class folder or JAR file. To create the file:

  1. Create an ASCII text file named javaclasspath.txt.

  2. Enter the name of a Java class folder or JAR file, one per line. The format of the name depends on how the class is defined.

  3. Simplify folder specifications in cross-platform environments by using the $matlabroot, $arch, and $jre_home macros.

  4. Save the file in your preferences folder. To view the location of the preferences folder, type:

    prefdir

    Alternatively, save the javaclasspath.txt file in your MATLAB startup folder. To identify the startup folder, type pwd at the command line immediately after starting MATLAB. Classes specified in the javaclasspath.txt file in the startup folder appear on the path before classes specified in the file in the preferences folder. If a class appears in more than one folder or jar file, then Java uses the first one it finds.

  5. Restart MATLAB.

MATLAB reads the static class path only at startup. If you edit javaclasspath.txt or change your .class files while MATLAB is running, then restart MATLAB to put those changes into effect.

If you do not want MATLAB to use the entries in the javaclasspath.txt file, then start MATLAB with the -nouserjavapath option.

For information about using the dynamic class path when writing your own Java classes, see Dynamic Path of Java Class Path. For information about the startup folder, see MATLAB Startup Folder.

Add Individual (Unpackaged) Classes

To make individual classes—classes that are not part of a package—available in MATLAB, specify the full path to the folder containing the .class files. For example, for a compiled Java class in the file c:\work\javaclasses\test.class, add the following entry to the javaclasspath.txt file.

c:\work\javaclasses

Add Packages

To make a package available to MATLAB, specify the full path to the parent folder of the highest level folder of the package path. This folder is the first component in the package name. For example, if your Java class package com.mw.tbx.ini has its classes in folder c:\work\com\mw\tbx\ini, add the following entry to the javaclasspath.txt file.

c:\work

Add JAR File Classes

A JAR file contains multiple Java classes and packages in a compressed ZIP format. For information on the jar (Java Archive) tool and JAR files, consult your Java development documentation.

To make the contents of a JAR file available for use in MATLAB, specify the full path, including the full file name, for the JAR file. You also can put the JAR file on the MATLAB path.

Note

The path requirement for JAR files is different from the requirement for .class files and packages, for which you do not specify file names.

For example, you have a JAR file named mylibrary.jar in the folder C:\Documents\MATLAB\, containing a method, package.class.mymethod(params).

  • Edit the javaclasspath.txt file.

    cd(prefdir)
    edit javaclasspath.txt
  • Add the following text on a new line in the file.

    C:\Documents\MATLAB\mylibrary.jar
  • Save and close the file.

  • Restart MATLAB.

  • Call mymethod.

    package.class.mymethod(params)

Related Topics