Main Content

静态的路径JavaClass Path

The static path is loaded at the start of each MATLAB®session from the MATLAB built-in Java®path and thejavaclasspath.txtfile. 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 ajavaclasspath.txtfile. For instructions, seeCreate javaclasspath.txt File.

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

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

Createjavaclasspath.txtFile

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

  1. Create an ASCII text file namedjavaclasspath.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_homemacros.

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

    prefdir

    Alternatively, save thejavaclasspath.txtfile in your MATLAB startup folder. To identify the startup folder, typepwdat the command line immediately after starting MATLAB. Classes specified in thejavaclasspath.txtfile 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 editjavaclasspath.txtor change your.classfiles while MATLAB is running, then restart MATLAB to put those changes into effect.

If you do not want MATLAB to use the entries in thejavaclasspath.txtfile, then start MATLAB with the-nouserjavapathoption.

For information about using the dynamic class path when writing your own Java classes, seeDynamic Path of Java Class Path. For information about the startup folder, seeMATLAB的启动文件夹.

添加个人(未包装的)类

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

c:\work\javaclasses

Add Packages

To make a package available to MATLAB, specify the full path to theparent folder of the highest level folderof the package path. This folder is the first component in the package name. For example, if your Java class packagecom.mw.tbx.inihas its classes in folderc:\work\com\mw\tbx\ini, add the following entry to thejavaclasspath.txtfile.

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.classfiles and packages, for which you do not specify file names.

For example, you have a JAR file namedmylibrary.jarin the folderC:\Documents\MATLAB\, containing a method,package.class.mymethod(params).

  • Edit thejavaclasspath.txtfile.

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

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

  • Restart MATLAB.

  • Callmymethod.

    package.class.mymethod(params)

Related Topics

External Websites