Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8732077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:22:23+00:00 2026-06-13T09:22:23+00:00

In ruby I want to execute some local command which will generate a file,then

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:22:24+00:00Added an answer on June 13, 2026 at 9:22 am

    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

    t=Thread.new do
      exec "java -jar xx.jar --output main.txt";
    end
    

    So your code never run anything after this

    Try with system or backtick command

    something like this

    t=Thread.new do
      system "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
    

    Hope it help

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to execute some ruby code on the start of application. Actualy this
I want regular expression in ruby on rails which remove all the html tags
I have a query in my ruby file : @mastertest = connection.execute(select code_ver,date from
I've one rake task which i want to execute once every day: in production
I want to have a button that, when clicked, will execute rails code. Is
I want to write a script in Ruby to clean up some messed up
I would like to execute an OS command from my ruby script but I
This is the bit of Ruby I want to implement in Python: Base64.urlsafe_encode64([Digest::MD5.hexdigest(url).to_i(16)].pack(N)).sub(/==\n?$/, '')
Good afternoon! I am pretty new to Ruby and want to code a basic
I'm reading Metaprogramming Ruby and just want to clear something up about the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.