-
Notifications
You must be signed in to change notification settings - Fork 3
/
setupMatR.m
54 lines (45 loc) · 1.99 KB
/
setupMatR.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
% Not sufficient that these classes are in the dynamic path
function setupMatR()
jpath = javaclasspath('-static');
[~,jars] = cellfun(@(x) fileparts(x),jpath,'uni',0);
if any(strcmp(jars,'REngine')) && any(strcmp(jars,'RserveEngine'))
% Already in user's static javaclasspath
else
path = fileparts(which('MatR'));
% TODO, check existence, download otherwise
javaaddpathstatic([path filesep 'lib/REngine.jar']);
javaaddpathstatic([path filesep 'lib/RserveEngine.jar']);
end
function javaaddpathstatic(file, classname)
%JAVAADDPATHSTATIC Add an entry to the static classpath at run time
%
% javaaddpathstatic(file, classname)
%
% 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. Use
% the optional classname input arg to let Matlab know this class exists.
% * There is no way to remove the new path entry once it is added.
% Andrew Janke 20/3/2014 http://stackoverflow.com/questions/19625073/how-to-run-clojure-from-matlab/22524112#22524112
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);
if nargin > 1
% load the class into Matlab's memory (a no-args public constructor is expected for classname)
eval(classname);
end