Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close command prompt using java, if already opened

Tags:

java

Scenario was i started the command prompt manually. Then i need to close the Command prompt automatically using Java. How i can close the command prompt using java. Any one can help me

like image 515
Saravanakumar Marudhachalam Avatar asked Dec 11 '22 13:12

Saravanakumar Marudhachalam


1 Answers

Use the following code:

class CmdKill
{
    public static void main(String args[])
    {
        try {
            Runtime.getRuntime().exec("taskkill /f /im cmd.exe") ;
        } catch (Exception e) {
            e.printStackTrace();  
        }
    }

}

Surely would help!!

Edit:

To kill a particular process, if u have the PID(from task manager.) you can use taskkill with /pid option.Refer for options here.But anyways you wont have the pid without looking into the task manager.The only solution seems as suggested by this SO answer that you start the process and get hold of the reference.

like image 178
rahulserver Avatar answered Jan 08 '23 05:01

rahulserver