This enhancement has been incorporated in Release 2006a (R2006a). For previous product releases, read below for any possible workarounds:
The following example demonstrates how a Java class from a JAR file can be used in MATLAB. It assumes you already have some knowledge of Java.
Three files are included with this example. They are:
1. HelloWorld.java - Java source code for HelloWorld.class.
2. HelloWorld.class - Compiled HelloWorld.class.
3. myHelloArchive.jar - JAR archive containing HelloWorld.class.
The first two files are included for reference. The third file, myHelloArchive.jar, is the JAR file containing the HelloWorld.class Java class, with which this example is concerned.
Step 1. Download the attached example file(s).
This example assumes that you will place myHelloArchive.jar in your $MATLAB\work directory (where the term $MATLAB represents the MATLAB root directory on your machine).
The other files, HelloWorld.java and HelloWorld.class, are also available for your reference.
Step 2. Add the JAR archive file to your Java class path.
When working with JAR archive files, the full name of the JAR file must be specified explicitly in the Java class path.
There are two parts to the Java class path: a static part that is loaded from a file at the start of each MATLAB session, and a dynamic part that can be loaded and modified during the MATLAB session through function calls.
The static Java class path is loaded from the following file:
To add our myHelloArchive.jar to your static Java path, edit classpath.txt by adding the following line to the top of the path listings:
The dynamic Java class path is modified using the following MATLAB commands:
To add myHelloArchive.jar to your dynamic Java path, issue the following command at the MATLAB command prompt:
You need only add the JAR file to either your static or dynamic Java class path.
For more information on the Java class path, issue the following command at the MATLAB command prompt:
Step 3. Verify that the JAR archive file is listed on your Java class path.
Issue the following command at the MATLAB command prompt:
You should see "$MATLAB\work\myHelloArchive.jar" listed under either your Static Java Path or Dynamic Java Path, depending on how you added it to your Java class path in Step 2.
Step 4. Make calls to the Java class(es) contained in your JAR file.
Now that you have the myHelloArchive.jar file on your Java class path, you should be able to use the HelloWorld class that it contains.
4a. Call Public Static Functions of the Class
Public static functions of a Java class can be called without having to create an instance of that class. For the HelloWorld class of the myHelloArchive.jar archive file, this can be done by issuing the following command at the MATLAB command prompt:
4b. Create an Instance of the Class
To create an instance of the HelloWorld class, issue the following command at the MATLAB command prompt:
myHelloObject = HelloWorld
4c. Call methods of the Class
The following code sequence shows different calls to the public methods of the myHelloObject object created in Step 4b.
For more information on calling Java from MATLAB, refer to the "Calling Java from MATLAB" section of the MATLAB documentation by issuing the following command at the MATLAB command prompt: