简体   繁体   English

为什么不能从 Java 执行包含 String[] 的 Julia 脚本

[英]why cannot execute Julia script which contains String[] from Java

public class ScriptProcess {

    private String[] command;

    public ScriptProcess(String[] c) {
        this.command = c;
    }

    public void exec() {
        try {
            Process p = Runtime.getRuntime().exec(this.command);
            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuffer stringBuffer = new StringBuffer();
            String line = null;
            while((line = in.readLine()) != null){          
                stringBuffer.append(line + " --- ");
            }
            System.out.println(stringBuffer.toString());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

I can execute Julia script with this Java class.我可以用这个 Java 类执行 Julia 脚本。

For example, here is a julia script:例如,这是一个 julia 脚本:

#main.jl
msgTest = Int64[]
push!(msgTest, 1)
push!(msgTest, 2)    
println(msgTest)

Then, I can initialize a ScriptProcess to execute the julia script above:然后,我可以初始化一个 ScriptProcess 来执行上面的 julia 脚本:

String[] command = {"C:\\Julia\\bin\\julia.exe", "C:\\doc\\Julia\\main.jl"};
ScriptProcess sp = new ScriptProcess(command);
sp.exec();

Everything's fine, I can get the result: [1,2] on the console of eclipse.一切正常,我可以在eclipse的控制台上得到结果: [1,2]

However, if I change the Int64 into String for the julia script like this:但是,如果我将 julia 脚本的Int64更改为String ,如下所示:

#main.jl
msgTest = String[]
push!(msgTest, "aaa")
push!(msgTest, "bbb")    
println(msgTest)

And I run the same java project, I can't get anything.而且我运行同一个java项目,我什么也得不到。

However, I can run the julia script with command prompt:但是,我可以使用命令提示符运行 julia 脚本:

C:\> C:\Julia\bin\julia.exe C:\doc\Julia\main.jl

I can get the result: AbstractString["aaa","bbb"] .我可以得到结果: AbstractString["aaa","bbb"]

The new version Julia will generate a warning if String is used.如果使用String ,新版本 Julia 将生成警告。 String has been changed to AbstractString . String已更改为AbstractString

If we use String in Julia or some IDE of Julia, things will still be fine except a warning.如果我们在 Julia 或 Julia 的某些 IDE 中使用String ,除了警告之外,一切都会好起来的。 However, if we want to execute a julia script with another language project, somehow we can't.但是,如果我们想用另一种语言项目执行 julia 脚本,不知何故我们不能。

Since not a direct answer to the question, your example code is the base logic under an existing library, namely JuliaCaller, which is distributed with an open source license.由于不是问题的直接答案,您的示例代码是现有库(即 JuliaCaller)下的基本逻辑,该库以开源许可证分发。 You can pass Java objects to Julia, make calculations on the Julia side and get back the result into Java.您可以将 Java 对象传递给 Julia,在 Julia 端进行计算并将结果返回给 Java。 Here is the link: JuliaCaller GitHub .这是链接: JuliaCaller GitHub

here is an example:这是一个例子:

List<Double> values = List.of(1.0, 2.0, 10.0, -4.0);
caller.addJuliaObject(JuliaObject.createArrayVariable("a", values));
caller.Execute("using Statistics");
caller.Execute("ave = mean(a)");
double result = caller.getDouble("ave");
assertEquals(2.25, result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM