从MATLAB调用Java [英] Calling Java from MATLAB

查看:435
本文介绍了从MATLAB调用Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 Swig 为以C ++编写的库创建Java包装器。包装器生成一个包,然后 jar 'ed。



我试图在静态Java路径文件中将路径添加到jar中。 MATLAB然后调用jar文件中的类,但我得到错误未定义的变量或类..或者如果我尝试使用 javaObject(。 ..) 没有类*可以位于Java类路径



我不确定我做错了什么。






编辑:



为了测试从MATLAB调用c ++库,我创建了一个简单的数据读取器类,它包含一个函数,用于写入随机生成的 vector< double>



生成的swig文件是: SimpleReader。 java DoubleVector.java exampleJNI.java 示例包 com.example.reader 中的.java DoubleVector2.java 。这些被编译并打包到 example.jar (生成的库dll也被打包到jar中)。



它一切正常工作从Java调用它所以问题必须具体到MATLAB。没有太多的MATLAB代码,因为没有什么似乎工作。我得到到

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

在这一点我得到'未定义变量com或类com .example.reader.SimpleReader'

解决方案

一般来说,这样做:

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

我已经使用这个与MATLAB相当一段时间了,没有麻烦。也许你可以发布你使用的确切的MATLAB代码?







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

在这一点我得到未定义变量com或类com.example.reader.SimpleReader '


好吧,对于初学者,你提到你的jarfile叫做 example.jar ,但是你的MATLAB代码引用 reader.jar - 你确定你在中引用的jar javaclasspath()存在?你试过看看它的内容吗? (例如使用7zip或任何可以读取.zip格式文件的程序,因为.jar文件只是带有附加规格的.zip格式文件)






hmmm ...




  • 您使用哪个版本的MATLAB?

  • <

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







您说您使用的是7.0.4版本,这可能是个问题。 早期版本的MATLAB使用较早版本的Java JRE


MATLAB仅在我们与MATLAB一起提供的JVM上完全支持。例如:



JVM 1.3.1 for MATLAB 6.5.1(R13SP1)



JVM 1.4.2 for MATLAB 7.0.4(R14SP1)

<> MATLAB 7.0.4(R14SP2)和更高版本,直到MATLAB 7.4(R2007a)使用JVM 1.5和MATLAB 7.5(R2007b) JVM 1.6。有一些组件可能无法在不同版本的JVM下正常工作。


此时您基本上有三个选择。 p>


  • (如果可能) - 只使用与Java 5兼容的JAR文件。在这种情况下,您需要使用自己的图书馆, code> -target 1.5 选项。 ( target =1.5如果你使用ant < javac> 任务)一个巨大的交易,因为1.6是从1.5增量改进 - 虽然如果你使用一些几个Java 6类,如 ArrayDeque ,或者依赖于1.6的外部库,你运气不好


  • 使用JRE 1.6搭配Matlab 7.4的更改JVM


  • 将MATLAB升级到在Java 6(R2007b或更高版本)上运行的版本。




当您将Java开发环境升级到Java 7或Java 8时,请记住此问题。


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"'

解决方案

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.

这篇关于从MATLAB调用Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆