| Contents | Index |
javaaddpath(dpath1,...,dpathN)
javaaddpath(dpath1,...,dpathN,'-end')
javaaddpath(dpath1,...,dpathN) adds one or more folders or JAR files to the beginning of the current dynamic Java class path.
javaaddpath(dpath1,...,dpathN,'-end') adds files or folders to the end of the path.
The Java path consists of two segments: a static path (read only at startup) and a dynamic path. The MATLAB software 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.
MATLAB calls the clear java command whenever you change 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. |
dpath1,...,dpathN |
String or cell array of strings specifying the folder or JAR file. |
This example demonstrates modifying the dynamic Java class path.
Create a function to set your initial dynamic 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 and then 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
Add the following entries to the dynamic path. One entry specifies a folder and the other a Java Archive (JAR) file. When you add a folder to the path, MATLAB includes all files in that folder as part of the path:
javaaddpath({
'C:\Work\Java\Curvefit\Test', ...
'C:\Work\Java\mywidgets.jar'});
Display the dynamic path:
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 defined on the dynamic path, you must clear the former definition for those classes from MATLAB memory. To clear all dynamic class definitions, type:
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.
Remove mywidgets.jarfrom the current dynamic class path:
javarmpath('C:\Work\Java\mywidgets.jar');
Add a JAR file from an internet URL to the Java path:
javaaddpath http://www.example.com/my.jar
Add the current folder:
javaaddpath(pwd)
clear | javaclasspath | javarmpath

Explore how to use MATLAB to make advancements in engineering and science.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |