静态的路径JavaClass Path
The static path is loaded at the start of each MATLAB®session from the MATLAB built-in Java®path and thejavaclasspath.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 ajavaclasspath.txt
file. 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.txt
File
Each line in thejavaclasspath.txt
file contains a reference to a Java class folder or JAR file. To create the file:
Create an ASCII text file named
javaclasspath.txt
.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.
For classes defined in Java packages, seeAdd Packages.
For classes defined in individual
.class
files, see添加个人(未包装的)类.For classes defined in Java Archive (JAR) files, seeAdd JAR File Classes.
Simplify folder specifications in cross-platform environments by using the
$matlabroot
,$arch
, and$jre_home
macros.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, typepwd
at the command line immediately after starting MATLAB. Classes specified in thejavaclasspath.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.Restart MATLAB.
MATLAB reads the static class path only at startup. If you editjavaclasspath.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 thejavaclasspath.txt
file, then start MATLAB with the-nouserjavapath
option.
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.class
files. For example, for a compiled Java class in the filec:\work\javaclasses\test.class
, add the following entry to thejavaclasspath.txt
file.
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.ini
has its classes in folderc:\work\com\mw\tbx\ini
, add the following entry to thejavaclasspath.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 namedmylibrary.jar
in the folderC:\Documents\MATLAB\
, containing a method,package.class.mymethod(params)
.
Edit the
javaclasspath.txt
file.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.
Call
mymethod
.package.class.mymethod(params)