0

I need to run the Ruby script using command line from Java code.

For example my file is in the path D:/MyProject/myruby.rb

I want to run this file from command line and get the response from that. How can I achieve this?

Also, how can we return the response in myruby.rb which could be caught in the command line.

4

1 回答 1

1

你可以使用这个:

String[] commands = {"ruby","D:/MyProject/myruby.rb"};

Runtime rt = Runtime.getRuntime();

Process proc;
try
{
    proc = rt.exec(commands);
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    String s;

    while ((s = stdInput.readLine()) != null) 
    {
        System.out.println(s);
    }
}
catch (IOException e)
{
    e.printStackTrace();
}
于 2013-08-07T17:26:48.940 回答