Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

class - Calling Java from MATLAB

I have been using Swig to create a Java wrapper for a a library written in C++. The wrappers get generated into a package and then jar'ed. The files are compiled correctly and work perfectly with java but I can't call it from MATLAB.

I tried adding the path to the jar in the static Java path file in MATLAB and then calling the classes in the jar file but I get the error "Undefined variable or class.." Or if I try using javaObject(...) "No class * can be located on Java class path".

I'm not sure what I am doing wrong.


EDIT:

To test calling a c++ library from MATLAB, I created a simple "data reader" class which contains a function that writes a randomly generated vector< vector<double> > to a text file and and a function that reads it.

The swig files generated are: SimpleReader.java, DoubleVector.java, exampleJNI.java, example.java, DoubleVector2.java in the package com.example.reader. These are compiled and packed into example.jar (the library dll generated is also packed into the jar).

It all works fine calling it from java so the problem must be specific to MATLAB. There is not much code for MATLAB as nothing seems to work. I get as far as

javaclasspath('c:/reader/reader.jar');
obj = com.example.reader.SimpleReader;

at which point I get 'Undefined variable "com" or class "com.example.reader.SimpleReader"'

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In general you're supposed to be able to do this:

javaclasspath('/path/to/myjar.jar')
myobj = com.example.mypackage.MyObject;
myobj.someMethod(123);

I've been using this with MATLAB for quite a while now and have had no trouble. Perhaps you could post the exact MATLAB code you are using?


I get as far as

javaclasspath('c:/reader/reader.jar'); 
obj = com.example.reader.SimpleReader; 

at which point I get 'Undefined variable "com" or class "com.example.reader.SimpleReader"'

Well, for starters, you mentioned your jarfile is called example.jar, but your MATLAB code references reader.jar -- are you sure the jar you're referencing in javaclasspath() exists? Have you tried looking at the contents of it? (e.g. with 7zip or any program that can read .zip-format files, since .jar files are just .zip-format files with additional specifications)


hmmm...

  • which version of MATLAB are you using?

  • are your classes public?

  • What do you get when you try typing the following:

      javap -classpath c:/reader/example.jar com.example.reader.SimpleReader
    

You say you're using version 7.0.4 -- this is likely the problem. Earlier versions of MATLAB use an older version of the Java JRE:

MATLAB is only fully supported on the JVM that we ship with MATLAB. For example:

JVM 1.3.1 for MATLAB 6.5.1 (R13SP1)

JVM 1.4.2 for MATLAB 7.0.1 (R14SP1)

MATLAB 7.0.4 (R14SP2) and later versions till MATLAB 7.4 (R2007a) use JVM 1.5 and MATLAB 7.5 (R2007b) and later use JVM 1.6. There are components that may not work properly under a different version of the JVM.

You basically have three choices at this point.

  • (if possible) -- use only JAR files that are compatible with Java 5. In this case, since you're making your own library, you need to use the -target 1.5 option. (target="1.5" if you're using the ant <javac> task) This generally isn't a huge deal, since 1.6 is kind of an incremental improvement from 1.5 -- although if you're using some of the few Java 6 classes like ArrayDeque, or external libraries that depend on 1.6, you're out of luck.

  • use JRE 1.6 with Matlab 7.4 by changing the JVM. Not sure this is a good idea.

  • upgrade MATLAB to a version that runs on Java 6 (R2007b or later).

Remember this issue when you go to upgrade your Java development environment to Java 7 or Java 8.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...