在Java中执行命令julia.exe

时间:2015-11-26 12:54:39

标签: java exec julia

我正在尝试用Java执行julia.exe

以下是代码:

Process pTest = Runtime.getRuntime().exec("C:/Program Files/Julia-0.4.1/bin/julia.exe");

当我跑步时,没有任何反应。

但是,如果我尝试另一个可执行文件,它运行良好。例如:

Process pTest = Runtime.getRuntime().exec("C:/Program Files/anotherProgram/program.exe");

program.exe将按预期运行。

julia.exe有点特别 如果我在命令提示符下运行它,它将在命令提示符下执行。换句话说,它不会弹出自己的窗口。

我做了一个测试:

#julia script, it's path: C:/Users/Thomas/Julia/test.jl
function test1()
  println("it's test1")
end

test1()

我在命令提示符下执行此命令:

C:\>C:/Program Files/Julia-0.4.1/bin/julia.exe C:/Users/Thomas/Julia/test.jl

然后我会在命令提示符下得到it's test1

我需要的是在我的java项目中执行C:/Program Files/Julia-0.4.1/bin/julia.exe C:/Users/Thomas/Julia/test.jl并在eclipse的控制台上获取it's test1

这是我的java项目:

public class Main {
    public static void main(String[] args){
        try {

        String[] params = {"C:/Program Files/Julia-0.4.1/bin/julia.exe", "C:/Users/Thomas/Julia/test.jl"};

        Process pTest = Runtime.getRuntime().exec(params);
        try {
            if (pTest.waitFor() != 0) {
                System.err.println("exit value = " + pTest.exitValue());
            }
            BufferedReader in = new BufferedReader(new InputStreamReader(pTest.getInputStream()));
            StringBuffer stringBuffer = new StringBuffer();
            String line = null;
            while ((line = in.readLine()) != null) {
                stringBuffer.append(line+"-");
            }
            System.out.println(stringBuffer.toString());
        } catch (InterruptedException e) {
            System.err.println(e);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

2 个答案:

答案 0 :(得分:1)

考虑这个已更改(并且正常工作)的实现,删除了#include <stdio.h> #include <stdexcept> #include <string> // Easy access to literals using namespace std::literals; // File wrapper class File { private: // The wrapped file FILE *_file; public: File(const char *name) : _file(fopen(name, "r")) { // unable to open the file? if (!_file) throw std::runtime_error{ "Unable to open file: "s + name }; } ~File() { fclose(_file); } // Convert to the underlying wrapped file operator FILE *() & { return _file; } // TODO: Member functions for working with the file }; waitFor的过早调用:

exitValue

这会使用您的测试脚本生成以下输出:

public class Main {
  public static void main(String[] args) {
    try {

        String[] params = {"C:/Program Files/Julia-0.4.1/bin/julia.exe", 
              "C:/Users/Thomas/Julia/test.jl"};

        Process pTest = Runtime.getRuntime().exec(params);
        BufferedReader in = new BufferedReader(new InputStreamReader(pTest.getInputStream()));
        String line = null;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        System.out.println("===");
        System.out.println("Julia exit value = " + pTest.exitValue());

    } catch (IOException e) {
        e.printStackTrace();
    }
  }
}

答案 1 :(得分:0)

我终于明白了。

julia.exe立即执行命令提示时,我们必须为cmd.exe的用户授予管理员权限。