简体   繁体   English

MATLAB:导入Java库

[英]MATLAB: importing java libraries

I have the following lines in my MATLAB code: 我的MATLAB代码中包含以下几行:

javaaddpath('C:\Users\localadmin\SkyDrive\IdeaProjects\DrawModels\out\artifacts\DrawModels_jar\DrawModels.jar')
javaaddpath('C:\Users\localadmin\SkyDrive\IdeaProjects\DrawModels\out\artifacts\DrawModels_jar\itextpdf-5.4.1.jar')

I then go on to instantiate some JAVA objects defined in the files. 然后,我继续实例化文件中定义的一些JAVA对象。

I would like to copy the JAR files to where the .m file sits, and then write the following instead (ie I want to use a relative path rather than an absolute one). 我想将JAR文件复制到.m文件所在的位置,然后编写以下内容(即,我想使用相对路径而不是绝对路径)。

javaaddpath('DrawModels.jar')
javaaddpath('itextpdf-5.4.1.jar')

However, the MATLAB path doesn't seem to apply to javaaddpath and so the JAR files do not get loaded. 但是,MATLAB路径似乎不适用于javaaddpath ,因此不会加载JAR文件。

Any ideas as to how I may achieve a relative path addressing of JAR files? 关于如何实现JAR文件的相对路径寻址的任何想法?

The background for this is I want the MATLAB code to run both on Windows and Linux, ie whereever it is unpacked. 这样做的背景是我希望MATLAB代码可以在Windows和Linux上运行,也就是无论它在哪里打包。 The user may unpack it in any folder he chooses, so I don't want a hard-coded path in the source. 用户可以将其解压缩到他选择的任何文件夹中,因此我不需要源中的硬编码路径。

Any help appreciated. 任何帮助表示赞赏。

You can use mfilename to find the path of the running M file, which you can concatenate with the JAR file name like, 您可以使用mfilename查找正在运行的M文件的路径,并将其与JAR文件名连接起来,例如,

javaaddpath(fullfile(fileparts(mfilename('fullpath')),'DrawModels.jar'))

That must go in the M file with which you would like to add the Java class to MATLAB. 那必须放在您要用来将Java类添加到MATLAB的M文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM