External Interfaces Reference |
Add Java classes to MATLAB using dynamic Java class path
Syntax
Description
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.
Remarks
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. |
Examples
Create function to set initial dynamic Java class path:
function setdynpath javaclasspath({ 'User4:\Work\Java\ClassFiles', ... 'User4:\Work\JavaTest\curvefit.jar', ... 'User4:\Work\JavaTest\timer.jar', ... 'User4:\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 User4:\Work\Java\ClassFiles User4:\Work\JavaTest\curvefit.jar User4:\Work\JavaTest\timer.jar User4:\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:
Use javaclasspath
with just an output argument to return the dynamic path alone:
p = javaclasspath p = 'User4:\Work\Java\ClassFiles' 'User4:\Work\JavaTest\curvefit.jar' 'User4:\Work\JavaTest\timer.jar' 'User4:\Work\JavaTest\patch.jar' 'User4:\Work\Java\Curvefit\Test' 'User4:\Work\Java\mywidgets.jar'
Create an instance of the mywidgets
class that is defined on the dynamic path:
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,
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:
Other Examples
Add a JAR file from an internet URL to your dynamic Java path:
Add the current directory with the following statement:
See Also
javaclasspath
, javarmpath
, clear
See Bringing Java Classes and Methods into MATLAB for more information.
isjava | javaArray |
© 1994-2005 The MathWorks, Inc.