2

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.

I could use screen -AmdS screen_thread123 php script.php but the main ideea is that i learn and understand how threads work.

I have a basic knowledge about this, but right now i want to create a quick dynamic thread like the example of bellow :

public static void exec_command_background(String command) throws IOException, InterruptedException
{

    List<String> listCommands = new ArrayList<String>();
    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();
}

and it gives me this error

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     

Why is that and how can i solve it? I mean how can i access the variable listCommands from this block?

new Thread(new Runnable(){
        public void run()
        {
            try
            {
                // code here
            }
            catch (IOException e)
            {
            }
            finally
            {
            }
        }
    }).start();
}

Thanks.

1 Answer 1

1

You don't need that inner class (and you don't want to waitFor)... just use

for(String element : arrayExplodedCommands)
{
    listCommands.add(element);
}
ProcessBuilder ps = new ProcessBuilder(listCommands);

ps.redirectErrorStream(true);
Process p = ps.start();
// That's it.

As for your question of accessing the variable listCommands in your original block; make the reference final - like so

final List<String> listCommands = new ArrayList<String>();
7
  • Yes, you're right, you don't have to wait for, but let's say i wanted to use threads, how the code will look like?
    – Damian
    Dec 23, 2013 at 16:09
  • @Damian ProcessBuilder already runs in a separate thread for you. Note that you call start() on it. Dec 23, 2013 at 16:11
  • I tested both. Work wonderfull. Thank you so muuch. I didn't used final before. What i know about final is that you can't change it anymore.
    – Damian
    Dec 23, 2013 at 17:05
  • Can't change the reference. You can still add items to your List. Dec 23, 2013 at 17:05
  • I read here en.wikipedia.org/wiki/Final_(Java) and i quote the final keyword is used in several different contexts to define an entity which cannot later be changed. , perhaps you said that you can still add items to a final List i have to try some tests :)
    – Damian
    Dec 23, 2013 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.