看起来这是 Clojure 不乐意从 Matlab 的“动态类路径”运行的问题。我使用捆绑的 JVM 或 Java 1.7.0u51 在 OS X 10.9 上使用 Matlab R2014a 时遇到了同样的错误。但是,如果我clojure-1.5.1.jar
通过将静态类路径放在javaclasspath.txt
Matlab 启动目录中的自定义中来添加它,那么 Clojure 类就会变得可见。
>> version -java
ans =
Java 1.7.0_51-b13 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
>> cloj = clojure.lang.RT
cloj =
clojure.lang.RT@77de6590
破解 Java 类路径
您在此答案中使用“类路径黑客”方法从 Matlab 命令行向静态类路径添加条目,而不必使用自定义的 Matlab 设置。那里的答案涉及编写一个新的 Java 类,但您可以在纯 M 代码中执行等效操作。
function javaaddpathstatic(file)
%JAVAADDPATHSTATIC Add an entry to the static classpath at run time
%
% javaaddpathstatic(file)
%
% Adds the given file to the STATIC classpath. This is in contrast to the
% regular javaaddpath, which adds a file to the dynamic classpath.
%
% Files added to the path will not show up in the output of
% javaclasspath(), but they will still actually be on there, and classes
% from it will be picked up.
%
% Caveats:
% * This is a HACK and bound to be unsupported.
% * You need to call this before attempting to reference any class in it,
% or Matlab may "remember" that the symbols could not be resolved.
% * There is no way to remove the new path entry once it is added.
parms = javaArray('java.lang.Class', 1);
parms(1) = java.lang.Class.forName('java.net.URL');
loaderClass = java.lang.Class.forName('java.net.URLClassLoader');
addUrlMeth = loaderClass.getDeclaredMethod('addURL', parms);
addUrlMeth.setAccessible(1);
sysClassLoader = java.lang.ClassLoader.getSystemClassLoader();
argArray = javaArray('java.lang.Object', 1);
jFile = java.io.File(file);
argArray(1) = jFile.toURI().toURL();
addUrlMeth.invoke(sysClassLoader, argArray);
因此,使用它javaaddpathstatic()
而不是javaaddpath()
您的代码可能会起作用。