Use my owe Java class in MATLAB

조회 수: 4 (최근 30일)
Ziyu Hua
Ziyu Hua 2022년 6월 24일
답변: Ziyu Hua 2022년 7월 2일
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
Ziyu Hua
Ziyu Hua 2022년 6월 25일
편집: Ziyu Hua 2022년 6월 25일
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 2022년 6월 25일
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)

댓글을 달려면 로그인하십시오.

채택된 답변

Ziyu Hua
Ziyu Hua 2022년 7월 2일
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.

추가 답변 (1개)

Sourav Sukumaran
Sourav Sukumaran 2022년 6월 29일
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
Ziyu Hua
Ziyu Hua 2022년 6월 30일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 External Language Interfaces에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by