title: Run BASH command in JAVA in background
body: <p>I made a function that executes a command from BASH, and i want to make it run in background and never stop the execution of the main program. </p>
  
  <p>I could use <code>screen -AmdS screen_thread123 php script.php</code> but the main ideea is that i learn and understand how threads work. </p>
  
  <p>I have a basic knowledge about this, but right now i want to create a quick dynamic thread like the example of bellow :</p>
  
  <pre><code>public static void exec_command_background(String command) throws IOException, InterruptedException
  {
  
      List&lt;String&gt; listCommands = new ArrayList&lt;String&gt;();
      String[] arrayExplodedCommands = command.split(" ");
  
      // it should work also with listCommands.addAll(Arrays.asList(arrayExplodedCommands));
      for(String element : arrayExplodedCommands)
      {
          listCommands.add(element);
      }
  
      new Thread(new Runnable(){
          public void run()
          {
              try
              {
                  ProcessBuilder ps = new ProcessBuilder(listCommands);
  
                  ps.redirectErrorStream(true);
                  Process p = ps.start();
  
                  p.waitFor();
              }
              catch (IOException e)
              {
              }
              finally
              {
              }
          }
      }).start();
  }
  </code></pre>
  
  <p>and it gives me this error</p>
  
  <pre><code>NologinScanner.java:206: error: local variable listCommands is accessed from within inner class; needs to be declared final
  ProcessBuilder ps = new ProcessBuilder(listCommands);
  1 error     
  </code></pre>
  
  <p>Why is that and how can i solve it? I mean how can i access the variable <code>listCommands</code> from this block?</p>
  
  <pre><code>new Thread(new Runnable(){
          public void run()
          {
              try
              {
                  // code here
              }
              catch (IOException e)
              {
              }
              finally
              {
              }
          }
      }).start();
  }
  </code></pre>
  
  <p>Thanks.</p>