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

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

42 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 21 Jun 2024
Edited: MathWorks Support Team on 3 Jul 2024
This is able to be done since MATLAB R2006a. 
The following example demonstrates how a Java class from a JAR file can be used in MATLAB.
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).
You can get the MATLAB root directory by running the following command in the MATLAB command window:
>> matlabroot
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.
You only need to add the JAR file to either the static or dynamic Java class path. 
2a. Add the JAR to the Static Java Class Path:
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
2b. Add the JAR to the Dynamic Java Class Path
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'))
For more information on the Java class path, please see the following documentation: 
https://www.mathworks.com/help/matlab/matlab_external/java-class-path.html
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:
https://www.mathworks.com/help/matlab/using-java-libraries-in-matlab.html

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!

Go to top of page