title: Ruby force close IO.popen java process
body: <h2>How can I force quit/kill a java process that was stared with IO.popen(&quot;command&quot;, &quot;r+&quot;)?</h2>
  <p>I am running a script a small java program from ruby doing the following:</p>
  <pre><code>pipe = IO.popen(&quot;nice -n 19 java -Xmx2g -Djava.awt.headless=true -jar java_program.jar&quot;, 'r+')
  </code></pre>
  <p>Then I use stdio to send arguments back and forth, like this:</p>
  <pre><code>pipe.puts &quot;data&quot;
  result = pipe.gets
  </code></pre>
  <p>This works fine for most data I send, but for some the java process seems to lock up or something, and I would like to force close / kill the java process.</p>
  <p>I am currently doing the following, which does not seem to kill the java process (from another thread which watches over this stuff):</p>
  <pre><code>thepid = nil
  thepid = pipe.pid            if pipe.respond_to?(:pid)
  pipe.puts('')                if pipe.respond_to?(:puts) #This is the java_program's approach to closing, writing empty string to stdio will cause it to start it's shutdown procedure.
  pipe.close                   if pipe.respond_to?(:close)
  Process.kill('KILL', thepid) if thepid &amp;&amp; Process.getpgid( thepid )
  </code></pre>
  <p>The java process lingers and refuses to die. What can I do to actually force the process to exit (it uses lots of ram :( )</p>
  <h3>Also: Is there a cross platform way of doing this?</h3>