| MATLAB Function Reference | ![]() |
javaaddpath('dpath')
javaaddpath('dpath', '-end')
javaaddpath('dpath') adds one or more directories or JAR files to the beginning of the current dynamic Java class path. dpath is a string or cell array of strings containing the directory or JAR file. (See the Remarks section for a description of static and dynamic Java paths.)
javaaddpath('dpath', '-end') adds one or more directories or files to the end of the current dynamic Java path.
The Java path consists of two segments: a static path (read only at startup) and a dynamic path. MATLAB® always searches the static path (defined in classpath.txt) before the dynamic path. Java classes on the static path should not have dependencies on classes on the dynamic path. Use javaclasspath to see the current static and dynamic Java paths.
Use the clear java command to reload the classes defined on the dynamic Java path. This is necessary if you add new Java classes or if you modify existing Java classes on the dynamic path.
Path Type | Description |
|---|---|
Static | Loaded at the start of each MATLAB session from the file classpath.txt. The static Java path offers better Java class loading performance than the dynamic Java path. However, to modify the static Java path you need to edit the file classpath.txt and restart MATLAB. |
Dynamic | 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. |
Create function to set initial dynamic Java class path:
function setdynpath
javaclasspath({
'C:\Work\Java\ClassFiles', ...
'C:\Work\JavaTest\curvefit.jar', ...
'C:\Work\JavaTest\timer.jar', ...
'C:\Work\JavaTest\patch.jar'});
% end of file
Call this function to set up your dynamic class path. Then, use the javaclasspath function with no arguments to display all current static and dynamic paths:
setdynpath;
javaclasspath
STATIC JAVA PATH
D:\Sys0\Java\util.jar
D:\Sys0\Java\widgets.jar
D:\Sys0\Java\beans.jar
.
.
DYNAMIC JAVA PATH
C:\Work\Java\ClassFiles
C:\Work\JavaTest\curvefit.jar
C:\Work\JavaTest\timer.jar
C:\Work\JavaTest\patch.jar
At some later time, add the following two entries to the dynamic path. One entry specifies a directory and the other a Java Archive (JAR) file. When you add a directory to the path, MATLAB includes all files in that directory as part of the path:
javaaddpath({
'C:\Work\Java\Curvefit\Test', ...
'C:\Work\Java\mywidgets.jar'});
Use javaclasspath with just an output argument to return the dynamic path alone:
p = javaclasspath p = 'C:\Work\Java\ClassFiles' 'C:\Work\JavaTest\curvefit.jar' 'C:\Work\JavaTest\timer.jar' 'C:\Work\JavaTest\patch.jar' 'C:\Work\Java\Curvefit\Test' 'C:\Work\Java\mywidgets.jar'
Create an instance of the mywidgets class that is defined on the dynamic path:
h = mywidgets.calendar;
If you modify one or more classes that are defined on the dynamic path, you need to clear the former definition for those classes from MATLAB memory. You can clear all dynamic Java class definitions from memory using:
clear java
If you then create a new instance of one of these classes, MATLAB uses the latest definition of the class to create the object.
Use javarmpath to remove a file or directory from the current dynamic class path:
javarmpath('C:\Work\Java\mywidgets.jar');
Add a JAR file from an internet URL to your dynamic Java path:
javaaddpath http://www.example.com/my.jar
Add the current directory with the following statement:
javaaddpath(pwd)
javaclasspath, javarmpath, clear
See Bringing Java™ Classes and Methods into MATLAB® Workspace for more information.
![]() | j | javaArray | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |