我一直在使用 Swig 为用 C++ 编写的库创建 Java 包装器。包装器生成为一个包,然后进行 jar 处理。这些文件已正确编译并与 java 完美配合,但我无法从 MATLAB 调用它。
我尝试在 MATLAB 中的静态 Java 路径文件中添加 jar 的路径,然后调用 jar 文件中的类,但出现错误 “未定义的变量或类..”
或者如果我尝试使用javaObject(...)
“没有类 * 可以位于 Java 类路径上”
。
我不确定我做错了什么。
编辑:
为了测试从 MATLAB 调用 C++ 库,我创建了一个简单的“数据读取器”类,其中包含一个写入随机生成的 vector 的函数。矢量<双> >
到一个文本文件和一个读取它的函数。
生成的 swig 文件为:SimpleReader.java
、DoubleVector.java
、exampleJNI.java
、example.java
、 com.example.reader
包中的 DoubleVector2.java
。这些被编译并打包到 example.jar
中(生成的库 dll 也打包到 jar 中)。
从 java 调用它一切正常,因此问题必须特定于 MATLAB。 MATLAB 的代码并不多,因为似乎什么都不起作用。我得到的信息是
javaclasspath('c:/reader/reader.jar');
obj = com.example.reader.SimpleReader;
'未定义的变量“com”或类“com.example.reader.SimpleReader”'
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"'
发布评论
评论(1)
一般来说,你应该能够做到这一点:
我已经在 MATLAB 中使用它很长一段时间了,没有遇到任何问题。也许您可以发布您正在使用的确切 MATLAB 代码?
那么,对于初学者来说,您提到您的jar文件名为
example.jar
,但是您的 MATLAB 代码引用了reader.jar
- 您确定在javaclasspath()
中引用的 jar 存在吗?您尝试过查看其中的内容吗? (例如,使用 7zip 或任何可以读取 .zip 格式文件的程序,因为 .jar 文件只是具有附加规范的 .zip 格式文件)嗯...
您使用的是哪个版本的 MATLAB?
你的课程是公开的吗?
当您尝试输入以下内容时,您会得到什么:
您说您正在使用版本 7.0.4 - 这可能是问题所在。 早期版本的 MATLAB 使用旧版本的 Java JRE:
此时你基本上有三个选择。
(如果可能)——仅使用与 Java 5 兼容的 JAR 文件。在这种情况下,由于您正在创建自己的库,因此需要使用
-target 1.5
选项。 (target="1.5"
如果您使用的是 ant
任务)这通常不是什么大问题,因为 1.6 是一种渐进式改进从 1.5 开始——尽管如果您使用的是少数几个 Java 6 类,例如ArrayDeque
,或依赖于 1.6 的外部库,你运气不好。通过更改 JVM 将 JRE 1.6 与 Matlab 7.4 结合使用。不确定这是个好主意。
将 MATLAB 升级到在 Java 6(R2007b 或更高版本)上运行的版本。
当您将 Java 开发环境升级到 Java 7 或 Java 8 时,请记住这个问题。
In general you're supposed to be able to do this:
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?
Well, for starters, you mentioned your jarfile is called
example.jar
, but your MATLAB code referencesreader.jar
-- are you sure the jar you're referencing injavaclasspath()
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:
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:
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 likeArrayDeque
, 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.