如何从 matlab 运行 clojure 脚本?
我尝试了以下操作:
使用jdk 1.7运行matlab,然后调用java
MATLAB_JAVA=/usr/lib/jvm/java-7-oracle/jre matlab
在matlab中,设置类路径并使用clojure编译器
javaaddpath([pwd '/lib/clojure-1.5.1.jar'])
import clojure.lang.RT
这里我得到了错误:
Error using import
Import argument 'clojure.lang.RT' cannot be found or cannot be imported.
当我编写运行 clojure 的 java 类时,一切都可以从控制台运行,但不应该从 matlab 运行。
请指教。
看来这是 Clojure 不乐意从 Matlab 的“动态类路径”运行的问题。我在 OS X 10.9 上使用捆绑的 JVM 或 Java 1.7.0u51 时,在 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 类路径
您使用“类路径黑客”方法这个答案 https://stackoverflow.com/questions/1010919/adding-files-to-java-classpath-at-runtime从 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()
并且您的代码可能会起作用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)