如何从matlab运行clojure

Bor*_*s T 3 matlab clojure classpath

如何从matlab运行clojure脚本?

我试过以下:用jdk 1.7运行matlab然后调用java

MATLAB_JAVA=/usr/lib/jvm/java-7-oracle/jre matlab
Run Code Online (Sandbox Code Playgroud)

在matlab中,设置classpath并使用clojure编译器

javaaddpath([pwd '/lib/clojure-1.5.1.jar'])
import clojure.lang.RT
Run Code Online (Sandbox Code Playgroud)

我在这里得到错误:

Error using import
Import argument 'clojure.lang.RT' cannot be found or cannot be imported. 
Run Code Online (Sandbox Code Playgroud)

当我编写运行clojure的java类时,一切都在从控制台运行,但是不能从matlab运行.请指教.

And*_*nke 6

看起来这是一个问题,Clojure不喜欢从Matlab的"动态类路径"运行.我在OS X 10.9上使用捆绑的JVM或Java 1.7.0u51在Matlab R2014a上遇到了同样的错误.但是如果我clojure-1.5.1.jar通过将它放在javaclasspath.txtMatlab启动目录中的自定义中来添加静态类路径,那么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
Run Code Online (Sandbox Code Playgroud)

破解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);
Run Code Online (Sandbox Code Playgroud)

因此,使用它javaaddpathstatic()而不是javaaddpath()代码可能会起作用.

  • 这里详细讨论:http://undocumentedmatlab.com/blog/static-java-classpath-hacks (2认同)