簡體   English   中英

如何從matlab運行clojure

[英]How to run clojure from matlab

如何從matlab運行clojure腳本?

我試過以下:用jdk 1.7運行matlab然后調用java

MATLAB_JAVA=/usr/lib/jvm/java-7-oracle/jre matlab

在matlab中,設置classpath並使用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添加到靜態類路徑中,方法是將它放在Matlab啟動目錄中的自定義javaclasspath.txt中,那么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() ,您的代碼可能會起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM