I use system "java -cp xxx.jar"
in my ruby script and it runs well on Mac OS. But when I run the script on Windows 7 x64, those java -cp xxx.jar
did not get executed and no error was reported.
views:
31answers:
2
+1
A:
system
doesn't throw an exception or anything if your command fails to run (which may be why you said "no error was reported").
So, you need to check whether java
is in your PATH
; by default on Windows, it's not, and you need to add the JDK's bin
directory to your PATH
.
Chris Jester-Young
2010-10-12 06:02:41
A:
Also your script can not be executed if you use several java classes in the classpath with ":"(colon) instead of ";"(semicolon) in Windows case;
classpath_separator = RUBY_PLATFORM =~ /mswin/ ? ';' : ':'
And if you want to catch output of the system command you can use next code:
def run_cmd cmd, cmd_name = 'Command'
# save current STDOUT reference
default_stdout = STDOUT.dup
# temp file used to capture output of the child processes
# and avoid conflicts between several processes running at the same time
# (temp file has a unique name and will be cleaned after close)
tmp_file = Tempfile.new 'tmp'
cmd_output = ''
puts "Begin #{cmd_name}: #{cmd}"
begin
# redirect default STDOUT to the tempfile
$stdout.reopen tmp_file
# execute command
system "#{cmd} 2>&1"
ensure
# read temp file content
tmp_file.rewind
cmd_output = tmp_file.read
tmp_file.close
# restore default STDOUT
$stdout.reopen default_stdout
end
# push output to console
puts "Output of #{cmd_name}: #{cmd_output}"
puts "End #{cmd_name}"
cmd_output
end
Tumtu
2010-10-12 13:42:08