In ruby I want to execute some local command which will generate a file,then I want to read the contents of the file,and modify it.
However I always get null.
This is the code:
exec "java -jar xx.jar --output main.txt";
content=IO.read('main.txt');
content="somethinkg:"+content;
File.open('main.txt','w') do |f|
f.puts content
end
the main.txt is generated,however I can not modify the content.
Then I think it maybe caused by the thread,when the main.txt is being read,the java command is not complete,so the content is empty.
Then I tried this:
t=Thread.new do
exec "java -jar xx.jar --output main.txt";
end
t.join
content=IO.read('main.txt');
puts content
content="somethinkg:"+content;
File.open('main.txt','w') do |f|
f.puts content
end
However nothing changed,I can not still get the content.
What is the problem?
I think the problem over here is the exec command If you check the ruby documentation on exec
over here
the code after exec never execute
so
So your code never run anything after this
Try with system or backtick command
something like this
Hope it help