简体   繁体   English

如何从命令行运行Ruby脚本并使用Java代码获取响应?

[英]How can I run a Ruby script from command line and get the response using Java code?

I need to run the Ruby script using command line from Java code. 我需要使用Java代码中的命令行来运行Ruby脚本。

For example my file is in the path D:/MyProject/myruby.rb 例如,我的文件位于路径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. 另外,我们如何在myruby.rb中返回可能在命令行中捕获的响应。

You can use this: 您可以使用此:

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();
}

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

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