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 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