Use my owe Java class in MATLAB

4 次查看(过去 30 天)
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 中查找有关 Call Java from MATLAB 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by