close windows command prompt after all process com

2019-09-02 04:34发布

I have two batch files I need to run. Two different java files are called in those two bat file.

Java files: J1.jar, J2.jar and are wrapped in J1.bat (xxx/java -jar J1.jar) and J2.bat Bath files: batch1.bat, batch2.bat executable programs: p1.exe, p2.exe, p3.exe, p4.exe

J1 will open a command prompt itself (cmd prompt window #1) and trigger batch2.bat (cmd prompt window #2) and after batch2 is done, J1 will do a couple of other things then exit. So I definitely need cmd prompt window #2 to close and return to cmd prompt window#1 so J1 can finish what it gotta do.

Right now, after batch2.bat is done, the window stays and obviously it does not return control to cmd prompt window #1.

Here are the two batch files

batch1.bat

call J1 batch2.bat
exit

batch2.bat

call J2 p1 p1_args
start /b J2 p2 p2_args    
start /b J2 p3 p3_args
call J2 p4 p4_args
exit

I have tried the following but none of them will close cmd prompt window#2

  • start J2 p2 p2 p2_args (this will open another window and does not close cmd prompt window#2
  • start "" J2 p2 p2_args (this will open another window and does not close cmd prompt window#2)
  • start /b cmd /c J2 p2 p2_args (this will not open a new window but does not close cmd prompt window#2)
  • use exit /b in batch2.bat (does not close cmd prompt window#2)

I also tried to put those two p2 and p3 in a separate bat file and call that bat file from batch2.bat, but that command prompt window still wouldn't close.

Is there a way to close the second command prompt window?? I heard of using auto hot key to do window scripting, is that a solution and a better way to do window scripting?

I gratefully appreciate any help. Thank you!

1条回答
时光不老,我们不散
2楼-- · 2019-09-02 05:21

After observation, I notice that after cmd took "exit", it will not return to the prompt and I will have to press enter to get things going. I decided to put a new line after the exit and it worked. I swapped out the exit with the following lines of code.

@echo off
set NLM=^
set NL=^^^%NLM%%NLM%^%NLM%%NLM%
exit %NL%

It's from this post, Explain how dos-batch newline variable hack works

查看更多
登录 后发表回答