Call Java from MATLAB

I am using Swig to create a Java wrapper for a library written in C ++. Wrappers are created in a package, and then jar 'ed. Files compiled correctly and work fine with java, but I can not name it from MATLAB.

I tried adding the path to the bank 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 to use javaObject(...) "No class * can be located on Java class path" .

I'm not sure what I'm doing wrong.


EDIT:

To test the C ++ library call from MATLAB, I created a simple data reading class that contains a function that writes a randomly generated vector< vector<double> > to a text file and a function that reads it.

Generated swig files: SimpleReader.java , DoubleVector.java , exampleJNI.java , example.java , DoubleVector2.java in the package com.example.reader . They are compiled and packaged in example.jar (the created DLL is also packaged in a jar).

Everything works fine, calling it from java, so the problem should be specific to MATLAB. There is little code for MATLAB since nothing works. I get to

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

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

+6
source share
1 answer

In general, you should do this:

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

I have been using this with MATLAB for quite some time now and have not experienced any problems. Perhaps you can post the exact MATLAB code you are using?


Reached to

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

at this moment I get the variable "Undefined" com "or the class" com.example.reader.SimpleReader "'

Well, for starters, you mentioned that your jarfile is called example.jar , but your MATLAB code is referenced by reader.jar - are you sure that the jar you reference in javaclasspath() exists? Have you tried looking at its contents? (for example, with 7zip or any program that can read files in .zip format, as .jar files are .zip files with additional specifications)


hmmm ...

  • What version of MATLAB are you using?
  • Are your classes publicly available?
  • What do you get when you try to enter the following:

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

You say that you are using version 7.0.4 - this is most likely a problem. Earlier versions of MATLAB use an earlier version of the Java JRE :

MATLAB is only supported on the JVM, which we ship with MATLAB. For instance:

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 prior to MATLAB 7.4 (R2007a) use JVM 1.5 and MATLAB 7.5 (R2007b), and then use JVM 1.6. There are components that may not work properly in another version of the JVM.

At the moment, you basically have three options.

  • (if possible) - use only Java 5 compatible JAR files. In this case, since you are creating your own library, you need to use the -target 1.5 parameter . ( target="1.5" if you use the ant <javac> task). This is usually not a huge deal, since 1.6 is a gradual improvement from 1.5 - although if you use some of the few Java 6 classes, such as ArrayDeque or external libraries depending on version 1.6, you're out of luck.

  • use JRE 1.6 with Matlab 7.4 modified JVM . Not sure if this is a good idea.

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

Keep this issue in mind when upgrading your Java development environment to Java 7 or Java 8.

+7
source

Source: https://habr.com/ru/post/894543/


All Articles