如何在Matlab中使用用户定义的Java类?

时间:2021-03-09 15:34:59

I have read the documentation and several websites on exactly how to do this, however Matlab does not seem to pick up the classes that I have added to the dynamic java class path. Nor do I use the right syntax to correctly construct the object.

我已经阅读了有关如何执行此操作的文档和几个网站,但是Matlab似乎没有选择我添加到动态java类路径中的类。我也没有使用正确的语法来正确构造对象。

I have an class HandDB and which to create an object of this type and invoke it's static methods to connect to a SQL database. The class has an empty constructor and takes no parameters. The class is part of a package 'nuffielddb' which I made in a project within Netbeans. All the files are on my usb stick which is my E:\ drive...

我有一个类HandDB,用于创建此类型的对象并调用它的静态方法来连接到SQL数据库。该类有一个空构造函数,不带参数。该类是我在Netbeans中的一个项目中创建的包'nuffielddb'的一部分。所有文件都在我的usb棒上,这是我的E:\驱动器......

I would like to be able to use all the classes within the package. The package is contained at E:\nuffielddb.

我希望能够使用包中的所有类。该软件包包含在E:\ nuffielddb中。

I entered the following commands into Matlab:

我在Matlab中输入了以下命令:

javaaddpath('E:\'); 
javaclasspath;    % Output from java class path includes E:\ within dynamic path
str = java.lang.String('Test'); % Works fine
db = nuffieldbd.HandDB(); % Does not work - undefined variable or class error

Interesting I typed 'import nuffielddb.*;' and received no error.

有趣的是我键入'import nuffielddb。*;'并没有收到任何错误。

Just where am I going wrong?

我哪里错了?

Thanks for your help btw!

谢谢你的帮助顺便说一下!

3 个解决方案

#1


1  

Ah problem solved! Well not solved in a sense! I found out it's actually a problem with my matlab installation and I have no idea how to fix it :-( Never mind, it works on the computers at the office :-)

啊问题解决了!从某种意义上说没有解决!我发现它实际上是我的matlab安装的一个问题,我不知道如何解决它:-(没关系,它适用于办公室的电脑:-)

#2


1  

if your classes are in a .jar file, make sure your classpath includes the .jar file name itself (not just the directory it's in).

如果您的类位于.jar文件中,请确保您的类路径包含.jar文件名本身(而不仅仅是它所在的目录)。

Also if the MATLAB JRE is Java 1.5 (R2006b is, whereas R2009a is Java 1.6, not sure when they switched), make sure your classes are compiled with 1.5 as a target, not 1.6, otherwise MATLAB will not be able to use them.

此外,如果MATLAB JRE是Java 1.5(R2006b是,而R2009a是Java 1.6,不确定何时切换),请确保您的类编译为1.5作为目标,而不是1.6,否则MATLAB将无法使用它们。

#3


1  

Minor note: .* imports will never error, so they're not diagnostic. They simply add a package to the list that Matlab searches through when trying to resolve a class name. Nonexistent packages are ignored.

次要注意:。*导入永远不会出错,因此它们不是诊断性的。他们只是将一个包添加到Matlab在尝试解析类名时搜索的列表中。不存在的包被忽略。

>> import this.package.does.not.exist.*
>> 

#1


1  

Ah problem solved! Well not solved in a sense! I found out it's actually a problem with my matlab installation and I have no idea how to fix it :-( Never mind, it works on the computers at the office :-)

啊问题解决了!从某种意义上说没有解决!我发现它实际上是我的matlab安装的一个问题,我不知道如何解决它:-(没关系,它适用于办公室的电脑:-)

#2


1  

if your classes are in a .jar file, make sure your classpath includes the .jar file name itself (not just the directory it's in).

如果您的类位于.jar文件中,请确保您的类路径包含.jar文件名本身(而不仅仅是它所在的目录)。

Also if the MATLAB JRE is Java 1.5 (R2006b is, whereas R2009a is Java 1.6, not sure when they switched), make sure your classes are compiled with 1.5 as a target, not 1.6, otherwise MATLAB will not be able to use them.

此外,如果MATLAB JRE是Java 1.5(R2006b是,而R2009a是Java 1.6,不确定何时切换),请确保您的类编译为1.5作为目标,而不是1.6,否则MATLAB将无法使用它们。

#3


1  

Minor note: .* imports will never error, so they're not diagnostic. They simply add a package to the list that Matlab searches through when trying to resolve a class name. Nonexistent packages are ignored.

次要注意:。*导入永远不会出错,因此它们不是诊断性的。他们只是将一个包添加到Matlab在尝试解析类名时搜索的列表中。不存在的包被忽略。

>> import this.package.does.not.exist.*
>>