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 Center および File ExchangeCall 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