How do I get a value returned by a Java method in MATLAB?

4 ビュー (過去 30 日間)
Bharath Lohray
Bharath Lohray 2015 年 10 月 28 日
編集済み: Bharath Lohray 2015 年 10 月 28 日
Hi,
I am trying to get value form a java class method. My Java code is as follows:
public class HelloWorld {
public static void main( String args[] )
{
System.out.println( "Hello World:" );
if (args.length >0){
System.out.println( args[0] );
}
}
public int f1 (String args[]){
if (args.length >0){
System.out.println( args[0] );
}
return 123;
}
}
When I try to execute the f1 method, it works as long as I am not attempting to get the value 123 to a MATLAB variable. What am I doing wrong? How do I fix this?
Trial>> o=HelloWorld
o =
HelloWorld@4a8b5227
Trial>> javaMethod('f1',o,'ABC')
ABC
Trial>> o.f1('ABC')
ABC
Trial>> a=javaMethod('f1',o,'ABC')
ABC
One or more output arguments not assigned during call to "javaMethod".
Trial>> a=o.f1('ABC')
ABC
One or more output arguments not assigned during call to "f1".
Trial>>

採用された回答

Bharath Lohray
Bharath Lohray 2015 年 10 月 28 日
編集済み: Bharath Lohray 2015 年 10 月 28 日
Never mind. I was iterating development and had the old class in memory. I had executed
clear classes
between tries, but I got my numbers when I executed
clear java
Here, I have the old implementation (returns 123) in memory.
Trial>> o.f1('ABC')
ABC
ans =
123
Then I change it to return 121
Trial>> clear classes
Trial>> o=HelloWorld
o =
HelloWorld@38d5b107
Trial>> o.f1('ABC')
ABC
ans =
123
Trial>>
To make this work, I executed `clear java`
Trial>> clear java
Trial>> javaaddpath .
Trial>> o=HelloWorld
o =
HelloWorld@273c947f
Trial>> o.f1('ABC')
ABC
ans =
121
Trial>>
Voilà!
I expected clear classes to work as after running `clear classes`, 'HelloWorld' ceased to show up in the output of `inmem`

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCall Java from MATLAB についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by