use java classes in matlab

5 ビュー (過去 30 日間)
Michael
Michael 2011 年 4 月 20 日
It should be pretty straightforward to get java classes working in matlab but I can't seem to do it...
I want to use functions from HttpClient ( http://hc.apache.org/httpcomponents-client-ga/ ) in my matlab program. I have put the 6 .jar files in the static (also tried dynamic) java path and restarted matlab but I can't call the functions.
I don't have experience with programming in java so it could be that I am making a mistake with my syntax.
Could somebody explain what I have to type in matlab exactly to call the functions?
Thank you very much!
  1 件のコメント
Jarrod Rivituso
Jarrod Rivituso 2011 年 4 月 20 日
Could you share with us the code that you have tried?

サインインしてコメントする。

採用された回答

Richard Alcock
Richard Alcock 2011 年 4 月 27 日
Try the following:
% Generate the list of jar files
jarDirectory = 'C:\Temp\httpcomponents-client-4.1.1\lib';
dirResult = dir([jarDirectory, filesep, '*.jar']);
jars = cellfun( @(name)fullfile(jarDirectory, name), ...
{dirResult.name}, 'UniformOutput', false );
% Add jars to the dynamic classpath
javaaddpath( jars );
% Check classpath
javaclasspath('-dynamic')
% Quick test that we can use classes from the library
httpclient = org.apache.http.impl.client.DefaultHttpClient()
Since the classes are added to the dynamic classpath, it is not persisted between restarts of MATLAB.
  1 件のコメント
Michael
Michael 2011 年 4 月 29 日
This really did the trick. Thank you very much!

サインインしてコメントする。

その他の回答 (1 件)

Neha
Neha 2011 年 5 月 21 日
Hey Richard,
I have written a Java program which should be invoked Matlab. I followed ur comments at link: http://www.mathworks.com/matlabcentral/answers/6923-calling-a-java-class-from-matlab Yet I'm not very clear about it. I have this simple HelloWorld program of Java:
class HelloWorld
{
public String Hello()
{
String helloWorld="Hello World!";
return helloWorld;
}
public static void main(String s[])
{
String helloWorld="Hello World!";
}
}
I wish to invoke use this reutned value of string further in matlab.
Plz help, how to invoke this.

カテゴリ

Help Center および File ExchangeCall Java from MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by