title: Running an interactive Python GUI (Matplotlib) from Java Process
body: <p>I have done quite a bit of research on this, and unfortunately have been unable to find an answer that is suitable to the problem I'm currently having.</p>
  
  <p>My question isn't as simple as "how do I run a python script from Java," to which there exists at least 10 threads all containing the same answer. I understand how to do that. My problem is as follows:</p>
  
  <p>I have a .py file (<code>gui.py</code>) which looks something like this:</p>
  
  <pre><code>#!/usr/bin/env python
  import matplotlib.pyplot as plt
  import copy
  import math
  import numpy as np
  
  class GUI(object):
      ...
      ...
      ...
  if __name__ == '__main__':
      GUI.run()
  </code></pre>
  
  <p>This gui is an interactive matplotlib plot on which users draw stuff. Details aren't important - bottom line is the script runs for as long as the matplotlib window is open.</p>
  
  <p>Now, I'm building a Java app on the side, and there's a button which when clicked creates a Java <code>Process</code> object which will call the script. I know from reading other posts that PIPE cannot be called from the java runtime, so I've created a <code>.sh</code> file which wraps around this python script.</p>
  
  <p><code>shell.sh</code>:</p>
  
  <pre><code>#!/bin/sh
  python /mypath/to/the/gui.py
  </code></pre>
  
  <p>The Java to call the shell script:</p>
  
  <pre><code>view.getLaunchGuiButton().addActionListener((e) -&gt; {
  
              try {
                  String[] prcArgs = new String[] { "/mypath/to/shell.sh" };
                  Process proc = Runtime.getRuntime().exec(prcArgs);
  
              } catch (IOException e1 ) {
                  e1.printStackTrace();
              }
          });
  </code></pre>
  
  <p>The shell script is correct - when I run it from the terminal correctly everything functions as normal.</p>
  
  <p>I know that the Java is indeed executing the shell script, and calling the python script. There are no errors. I didn't set up any <code>BufferedReader</code> because there's nothing to read - I just need to GUI to open. I believe that the problem is that once the shell is executed, the "terminal" so-to-speak which is opened by the <code>Runtime</code> is immediately closed, hence it kills the python script and therefore the GUI.</p>
  
  <p>My question is, how do I keep the <code>Runtime</code> from simply running and immediately killing the python script (AKA closing the terminal)? </p>
  
  <p>Things I've already done:</p>
  
  <ul>
  <li><code>chmod +x ../../shell.sh</code></li>
  <li>Tried using <code>nohup /call/python/here &amp;</code> in the shell script. Didn't work.</li>
  <li>Tried using Java's <code>ProcessBuilder</code> instead of the <code>Runtime</code>. No difference.</li>
  </ul>
  
  <p>Any ideas? Thank you all in advance.</p>