Calling Java from Matlab

조회 수: 4 (최근 30일)
Kyle
Kyle 2012년 10월 17일
편집: per isakson 2018년 3월 14일
I've seen there are a number of descriptions about this on the web, but I seem to be running into trouble. I have a simple Java function that I have compiled successfully in Java
// Java Code
//*------------------------------------
package testfunction;
public class TestFunction
{
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
//*------------------------------------
and I am attempting to load and call from Matlab
%%Matlab Code
% ---------------------------------------
javaclasspath({
'C:\Users\username\Desktop\JavaFunctions\TestFunction', ...
'C:\Users\username\Desktop\JavaFunctions\TestFunction\dist\TestFunction.jar'...
});
import testfunction.*
import testfunction.TestFunction
% ----------------------------------------
I can't even get test function to import correctly, I'm sure I'm missing something I just don't know what. Any assistance is welcome

채택된 답변

Malcolm Lidierth
Malcolm Lidierth 2012년 10월 20일
You have an instance of the Java VM running already with MATLAB so you do not need a main entry point. For hello world, just use a standard static method.
package a.b;
public class TestFunction {
private TestFunction(){
}
public static void HelloWorld() {
System.out.println("Hello, World");
}
}
In MATLAB
javaaddpath...
a.b.TestFunction.HelloWorld()
To create an instance method:
package a.b;
public class TestFunction {
public TestFunction(){
}
public void HelloWorld() {
System.out.println("Hello, World");
}
}
In MATLAB
x=a.b.TestFunction();
x.HelloWorld();
  댓글 수: 1
Kyle
Kyle 2013년 5월 16일
thank you for the assistance

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sachin Ganjare
Sachin Ganjare 2012년 10월 18일
  댓글 수: 1
Kyle
Kyle 2013년 5월 16일
thanks for the links they helped with an additional issue

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Downloads에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by