exit the batch file after running using java

14,938

Solution 1

can't you add exit to your batch file to exit it's own command prompt. Uisng taskill seems overkill for just closing one command prompt, don't you think?

PS: I've never worked on batch files just the command prompt so I'm assuming it accepts the same commands.

Solution 2

Run the batch file with cmd.exe /c job.bat. The /c switch carries out the command and then terminates the command interpreter.

Solution 3

Here is the solution that works: to close command window after executing the commands from the batch (.bat) file you need to add "exit" (without the quotes) in a new line of your batch file. If you want to delay the execution this is the way and it works:

public class TestSleep 
{
    public static void main ( String [ ] args ) 
    {
         System.out.println("Do this stuff");
         try 
         { 
            Thread.currentThread().sleep(3000); 
         }
         catch ( Exception e ) { }
         System.out.println("Now do everything after this");
     } 
}

Cheers

Solution 4

If you start the batch file with Runtime.exec(), it returns you a Process object. Calling the destroy() method will kill that process.

Share:
14,938
Admin
Author by

Admin

Updated on July 16, 2022

Comments

  • Admin
    Admin almost 2 years

    I know how to start the batch file using java code. When i run the batch file command prompt is opened. To close the command prompt i am using the taskill /im cmd.exe. but the problem is that the command prompt that is used to start the jboss is also closed. i want to kill the cmd with a particular process id. How do i get the process id of a particular cmd promt and kill that using java