Clear Filters
Clear Filters

Use my owe Java class in MATLAB

10 views (last 30 days)
Ziyu Hua
Ziyu Hua on 24 Jun 2022
Answered: Ziyu Hua on 2 Jul 2022
Hello everyone,
I want to use a Java class in my MATLAB code, which is called ''. I have compiled it with Java8 and the output 'Myclass.class' is in the folder 'C:\Documents\MATLAB\'.
Here is my Java code.
package com.Myclass;
public class JavaMatlab {
public void Myfunction(){
//My function code
I want to use this class and its methods in MATLAB and my code is as below:
import com.Myclass.*;
But it gives an error, it tells me MATLAB cannot find class com.Myclass.JavaMatlab in Java class path.
The version -java gives 'Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode'
version -java
The Java version of my computer is 1.8.0_333-b02.
Can you help me settle this problem? Thanks.
Ziyu Hua
Ziyu Hua on 25 Jun 2022
Edited: Ziyu Hua on 25 Jun 2022
Hi everyone,
It seems that if I do not put this class inside a package, than the javaObject function works.
public class JavaMatlab {
public void Myfunction(){
//My function code
Than compiled into JavaMatlab.class and fellow the process above, it works!
But in most of the Third party Java code, the package name is always exist, so how can I import a java class when it is inside a package?
Ziyu Hua
Ziyu Hua on 25 Jun 2022
If the Java enviroment of your MATLAB is not the same version as the Java in your OS, than you can change the Matlab Java enviroment in the 'System Environment Variable'.
First, create tow new System Environment Variable called JAVA_HOME and MATLAB_JAVA like this:
Where the JAVA_HOME is the installation folder of your JAVA.
After restarting your MATLAB and using 'version -java', you will get a exactly same JVM verion as it in your oprating system.
version. (From 'Java 1.8.0_202-b08' to 'Java 1.8.0_333-b02' in my MATLAB, Java version in Windows can be got use 'java -version' in cmd)

Sign in to comment.

Accepted Answer

Ziyu Hua
Ziyu Hua on 2 Jul 2022
Hello everyone,
I have solved this problem as below. It may not be the best solution but usable.
  1. First, open your package in idea and import the package.
  2. Build the module.
  3. Run javaaddpath in Matlab, the path is the one where the compiled xxx.class is exist in the 'out' folder of idea.(see blow)
  4. If the class is inside a package, then use:
var = javaObject('packageName.className');
Now you can use the Java class in package.

More Answers (1)

Sourav Sukumaran
Sourav Sukumaran on 29 Jun 2022
I understand that without the class being in a package the javaObject function works. Please refer to this article :
wherein a java function gets compiled into a package using library compiler.
  1 Comment
Ziyu Hua
Ziyu Hua on 30 Jun 2022
Thanks Sourav,
In this link, it gives how to compile a Matlab function into Java package. It really useful but cannot settle my problem. What I mean is I have a third party Java class (eg. Download from GitHub) and this Java class was packaged inside a package ( for example), and I want to use this Java class in my Matlab code.
Base on my test, using javaObject cannot create an Object if the class is inside a third party package, do you know how can I deal with this?

Sign in to comment.


Find more on External Language Interfaces in Help Center and File Exchange





Community Treasure Hunt

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

Start Hunting!