简体   繁体   中英

How can I run a ruby file in command line from Java?

I want to run a Ruby file in a given path, from Java.

For instance: C:\\Documents and Settings\\Administrator\\Desktop\\myruby.rb

In the command line i need to run the ruby file ruby myruby.rb

I have tried the following code but it's not working:

final String[] command = new String[]{"cmd", "/c", "C:\\Documents and Settings\\Administrator\\Desktop\\ruby myruby.rb"};

final Process process = Runtime.getRuntime().exec(command);

My myruby.rb file contains the following code:

puts "Hello"

The above builds sucessfully. But "Hello" is not printed.

Thank you for your help.

Have you tried putting the file name in quotes?

final String[] command = new String[]{"cmd", "/c",
       "\"C:\\Documents and Settings\\Administrator\\Desktop\\ruby muruby.rb\""};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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