In the Matlab code below I use Java to create a command-line interface to the external Julia program. Working in principle, I can experience (timing?) problems where the output is garbled, not complete, or delayed.
Are there better methods, how to assure the completion of the external program?
function obj = JuliaServer
% Use:
% jl = JuliaServer
% jl.start()
% jl.call('cos.(randn(3,3))')
% ...
% jl.stop()
import java.io.*
import java.net.*
%% properties
in = 0;
out = 0;
err = 0;
%% public methods
obj.start = @start;
obj.call = @call;
obj.stop = @stop;
%% methods
function start
rt = java.lang.Runtime.getRuntime();
pr = rt.exec('julia -iq');
in = BufferedReader(InputStreamReader(pr.getInputStream()));
out = PrintWriter(pr.getOutputStream());
err = BufferedReader(InputStreamReader(pr.getErrorStream()));
end
function [res, errmsg] = call(cmd)
println(out, cmd);
flush(out);
while ~(ready(in) || ready(err)), end
res = '';
while (ready(in))
res = append(res, sprintf('%s\n', strtrim(char(readLine(in)))));
end
errmsg = '';
if ready(err)
errmsg = sprintf('cmd: ''%s''\n', cmd);
while (ready(err))
errmsg = append(errmsg, sprintf('%s\n', strtrim(char(readLine(err)))));
end
error(errmsg)
end
end
function stop
println(out, 'exit()');
out.flush();
end
end