Chani
Chani

Reputation: 5165

Does the main thread in java finish up before any processes it might create using Runtime class finish executing

I am using the Runtime class to execute a piece of installation of a software. However, its not working, meaning that, after I fire the job (which is created using the Runtime class), after sometime (which is very soon) the installation job just exits. I think the problem is that the main thread must be finishing up and thus killing the Process that is created using the Runtime class. Am I correct ? And what is the solution here ?

this is how I fire my job inside teh main class :

try
        {
            Runtime.getRuntime().exec(cmd);
        }                               
        catch(IOException e)    
        {   
            //add logging functionality
            e.printStackTrace();
        }

Soon after this command, the main function finishes.

There is no problem with the Runtime command. It works .. I can even see it starting(the installation taht I am firing thru the code) and then it suddenly exits.

Upvotes: 4

Views: 180

Answers (3)

Bohemian
Bohemian

Reputation: 425033

The process created is a child process for the main thread. If the main thread finishes, the process will be killed, similar to if you executed a command manually and pressed ctrlc or closed the window.

Upvotes: 1

Wivlaro
Wivlaro

Reputation: 1515

You might want to check out the java.lang.Process class. You probably want something like this:

        Process process = Runtime.getRuntime().exec(cmd);
        process.waitFor();

The subprocess may be receiving a SIGHUP and exiting.

EDIT:

In context, something like this, I would think:

    try
    {
        Process process = Runtime.getRuntime().exec(cmd);
        process.waitFor();
    }                               
    catch(IOException e)    
    {   
        //add logging functionality
        e.printStackTrace();
    }
    catch(InterruptedException e)    
    {   
        e.printStackTrace();
    }

Upvotes: 5

user1103589
user1103589

Reputation:

This is just a wild guess, but I think to program quit's because you have a error in your code(logical) maybe a while loop that exits to soon,

try to look good at the code that the Runtime executes.

The process is probably in a deadlock. The solution is in the comments, with a sample.

Upvotes: 0

Related Questions