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

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

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by