calling a java class from matlab

I have a java class, and I need to call it from matlab to implement a document. is that you have an idea?

 Respuesta aceptada

Richard Alcock
Richard Alcock el 6 de Mayo de 2011
It depends on whether it has been packaged into a .jar file or is a .class file.
Your class is called hello.HelloWorld
If it's in a file called c:\work\hello\HelloWorld.class, you need to add the root directory to the class path.
javaaddpath c:\work\
function Test()
hello.HelloWorld();
end
On the other hand, if it's in a c:\work\hello.jar file, you add the jar file to the class path.
javaaddpath c:\work\hello.jar
After that's it's the same.

11 comentarios

najmaf najma
najmaf najma el 6 de Mayo de 2011
I add the path and when I call:
{
% Construct an instance
obj = najmaf.Najma;
}
it gives me this error:
??? Undefined function or variable 'HelloWorld'.
najmaf najma
najmaf najma el 6 de Mayo de 2011
HelloWorld it's my class file
Richard Alcock
Richard Alcock el 7 de Mayo de 2011
What is the full name of your class?
Is it in a jar file?
najmaf najma
najmaf najma el 7 de Mayo de 2011
No, is a simple Java class called: HelloWorld.class
this is the code:
{
package hello;
import java.lang.String;
public class HelloWorld {
//package mypack;
public String hello;
public HelloWorld() {
hello = "world";
}
}
}
i called this class from matlab with:
{
function Test()
test = HelloWorld();
end
}
it gives me the error:
{
??? Undefined function or variable 'HelloWorld'.
Error in ==> Test at 3
test = HelloWorld();
}
najmaf najma
najmaf najma el 8 de Mayo de 2011
do you have any suggestion???
najmaf najma
najmaf najma el 8 de Mayo de 2011
do you have any suggestion???
Richard Alcock
Richard Alcock el 8 de Mayo de 2011
I've updated my answer.
najmaf najma
najmaf najma el 8 de Mayo de 2011
I received nothing.
I sent you the class with which I work with the error I received
najmaf najma
najmaf najma el 14 de Mayo de 2011
is that you can send me your reply again because I have not received the update that you did
Neha
Neha el 21 de Mayo de 2011
Hey Richard,
I have this query I'm troubling since long with it.
I need to invoke a java class in Matlab, and need to store the returned string value in a variable in Mtalab. for this query i got following solution from a website, plz tell me where its going wrong:
>>
The Hello World solution by The MathWorks provides some insights on how to run a simple 'Hello World' java application inside MATLAB. You may change the Java code a bit, in order to have a method that returns a String.
public class HelloWorld
{
public String hello()
{
String helloWorld = "Hello World!";
return helloWorld;
}
}
Once this simple class is compiled and on the MATLAB JVM classpath create an instance and invoke the method with the following two commands.
o = HelloWorld
output = o.hello;
The String returned by the HelloWorld instance is assigned to the MATLAB variable output.
<<
The command described to invoke java code in matlab(ie o=HelloWorld) gives error.
Richard Alcock
Richard Alcock el 24 de Mayo de 2011
I need some more information:
1. What is the full path to HelloWorld.class file?
2. What is the exact javaaddpath command you ran in MATLAB?
3. What error are you getting from MATLAB

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by