Use my owe Java class in MATLAB

4 visualizaciones (últimos 30 días)
Ziyu Hua
Ziyu Hua el 24 de Jun. de 2022
Respondida: Ziyu Hua el 2 de Jul. de 2022
Hello everyone,
I want to use a Java class in my MATLAB code, which is called 'Myclass.java'. 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:
avaaddpath('C:\Documents\MATLAB\')
import com.Myclass.*;
Myclass=javaObject('com.Myclass.JavaMatlab');
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.
  2 comentarios
Ziyu Hua
Ziyu Hua el 25 de Jun. de 2022
Editada: Ziyu Hua el 25 de Jun. de 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 JavaMatlab.java 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 el 25 de Jun. de 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)

Iniciar sesión para comentar.

Respuesta aceptada

Ziyu Hua
Ziyu Hua el 2 de Jul. de 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.

Más respuestas (1)

Sourav Sukumaran
Sourav Sukumaran el 29 de Jun. de 2022
Hi
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.
Thanks
  1 comentario
Ziyu Hua
Ziyu Hua el 30 de Jun. de 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 (com.xxx 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?
Thanks.

Iniciar sesión para comentar.

Categorías

Más información sobre Call Java from MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by