How can I use a Java class from a JAR file in MATLAB?

86 views (last 30 days)
I have a Java class in a JAR file that I would like to use with MATLAB.
Do you have an example of how this can be done?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
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:
$MATLAB\toolbox\local\classpath.txt
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:
$matlabroot/work/myHelloArchive.jar
The dynamic Java class path is modified using the following MATLAB commands:
javaaddpath % Add entries to dynamic Java class path.
javarmpath % Remove entries from dynamic Java class path
clear java % Removes all variables, functions, MEX-files, and dynamic Java class definitions from memory, leaving the workspace empty.
To add myHelloArchive.jar to your dynamic Java path, issue the following command at the MATLAB command prompt:
javaaddpath(fullfile(matlabroot,'work','myHelloArchive.jar'))
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:
web([docroot '/techdoc/matlab_external/ch05mat5.html#4867'])
Step 3. Verify that the JAR archive file is listed on your Java class path.
Issue the following command at the MATLAB command prompt:
javaclasspath
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:
HelloWorld.main('') % Call public static functions of the HelloWorld class
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 % Create an instance of the class through the constructor
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.
myHelloObject.getVersion()
myHelloObject.setVersion(-1)
myHelloObject.getVersion()
myHelloObject.setVersion(1)
myHelloObject.getVersion()
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:
web([docroot '/techdoc/matlab_external/ch05matf.html'])
  1 Comment
Jaime Raldua
Jaime Raldua on 11 Sep 2017
it is worth mentioning that if your class is under a package you should create the instance as: myHelloObject = mypackage.HelloWorld

Sign in to comment.

More Answers (0)

MathWorks Support

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!