abc
abc

Reputation:

exit the batch file after running using java

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

Upvotes: 2

Views: 15584

Answers (6)

abi
abi

Reputation: 1

I too had the same problem. I first used as

Runtime.getRuntime().exec("cmd.exe start /c test.bat");

Then I tried as below. It works fine.

Runtime.getRuntime().exec("cmd.exe /c start test.bat");

Try this.

Upvotes: 0

Djole
Djole

Reputation: 31

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

Upvotes: 2

brayne
brayne

Reputation: 1405

Although this seems to be an old question and probably resolved.. I struggled with the same thing for a long time.. Finally this works

String command = "cmd.exe /c build.bat";
Runtime rt = Runtime().getRuntime();
Process pr = rt.exec(command);

Upvotes: 0

Maurice Perry
Maurice Perry

Reputation: 32831

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

Upvotes: 1

hhafez
hhafez

Reputation: 39750

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.

Upvotes: 3

Zach Scrivena
Zach Scrivena

Reputation: 29539

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

Upvotes: 3

Related Questions