Does the main thread execute in java before any processes that it can create using the runtime class execution

I use the Runtime class to complete the software installation part. However, it does not work, which means that after I run the task (which is created using the Runtime class), after a while (which is very soon), the installation task simply ends. I think the problem is that the main thread should end and thus kill the process created using the Runtime class. I'm right? And what is the solution here?

this is how i start my work inside the main class:

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

Shortly after this command, the main function ends.

No problem with the Runtime team. He works. I even see how it starts (installing taht I shoot through the code), and then it suddenly exits.

+4
source share
3 answers

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(); 

A subprocess can receive SIGHUP and exit.

EDIT:

In the context of 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(); } 
+5
source

The created process is a child process for the main thread. If the main thread ends, the process will be killed, as if you had executed the command manually and pressed ctrl c or closed the window.

+1
source

This is just a wild hunch, but I think the program is shutting down because you have an error in your code (logical), possibly in a while loop that will be coming out soon,

try looking good in the code executed by runtime.

The process is probably at a standstill. The solution is in the comments with the sample.

0
source

All Articles