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

4 vues (au cours des 30 derniers jours)
Bharath Lohray
Bharath Lohray le 28 Oct 2015
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>>

Réponse acceptée

Bharath Lohray
Bharath Lohray le 28 Oct 2015
Modifié(e) : Bharath Lohray le 28 Oct 2015
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`

Plus de réponses (0)

Catégories

En savoir plus sur Call Java from MATLAB dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by