31

Why won't cmd exit after execution of batch file?

I have tried:

"C:\Program Files (x86)\Java\jre6\bin\javaw.exe" -Xmx1024M -Xms1024M -jar Jilko.jar

and

@echo off
"C:\Program Files (x86)\Java\jre6\bin\javaw.exe" -Xmx1024M -Xms1024M -jar Jilko.jar
exit

12 Answers 12

38

If the Java app does not terminate (e.g. you are using the batch file to launch the Java app), then use the start command to launch it:-

start "" "C:\Program Files (x86)\Java\jre6\bin\javaw.exe" -Xmx1024M -Xms1024M -jar Jilko.jar

This will launch the java app and carry on executing the batch file without waiting for the java app to finish.

1
  • I finally got my script to run Visual Studio Code via the command line and close the cmd window afterward. But I wanted to point out that it was necessary to use the FULL PATH to the .exe instead of the code shortcut (or whatever it's called). For example start /b "" "<full-path-to-exe>" ... instead of start /b "" code .... Thanks!
    – Mycah
    May 5, 2023 at 16:39
22

Explanation:

Here is how it works; a batch file is processed one line at a time. Each command is executed in turn and the batch processor waits for one command to finish before starting the next. The problem you are experiencing is because the Java application you are launching (Jilko.jar) is a windowed program which continues to run even after the line that launches it. If it were a tool that performs some action and then terminates, the batch file would continue on to the next command (or exit if there are no more). Because the program is still running, the batch processor waits until the window is closed before moving on. You can see this in action by exiting the Java program: the console window with the batch file then closes.


Solution:

What you need to do to fix it, is to instruct the batch processor to launch the program and continue on without waiting as such:

start "" "C:\Program Files (x86)\Java\jre6\bin\javaw.exe" -Xmx1024M -Xms1024M -jar Jilko.jar

As Terrance mentioned, the "" is the title to use for the console window. However it is only optional if the command is not in quotes; otherwise is required. You can put something in there if you like or leave it empty, but if the command is in quotes, it must be present, otherwise the command-interpreter will treat the quoted command as the title and open a console that just sits there waiting for something to do.

You can use something the following command instead, but the quotes are just easier and safer since shortnames are not guaranteed to be the same on every system.

start C:\Progra~2\Java\jre6\bin\javaw.exe -Xmx1024M -Xms1024M -jar Jilko.jar

The start command is a built-in command that spawns a process (basically like running a program from the Start menu). So what happens in this context is that the batch processor runs the start command which in turn runs the specified program and terminates (itself, not the spawned program). As such, the batch processor continues on as expected. It also has some options that can be useful such as running the program minimized (/min) or maximized (/max), running it in low priority (/low) and so on. See start /? for details.

2
8

I have found that some of the programs I launch leave processes running, and the console window will not close until they terminate if I just run them by launching the executable.

The START program fixes that, but the old problem with START is still around. You can't just use:

START "c:\my dir\myfile.exe"

The first parameter of START is still the window name. If you omit it, you just open a CMD console box with the window named whatever you tried to launch. In the above instance, I'd now have a console window with the window title of "c:\my dir\myfile.exe". Not what I wanted!

To omit the window name, just use a pair of double quotes to define an empty string, such as:

START "" "c:\my dir\myfile.exe"

Finally, end your batch file with an EXIT command to ensure it closes.

This method seems to work consistently in Windows 7 and 8.

For troubleshooting, I find that adding an ECHO of what I'm about to do, then a TIMEOUT of what I just did, helps a lot. For example:

ECHO I'm about to launch the program...
START "" "c:\my dir\myfile.exe"
TIMEOUT 5

Since the Timeout gives you a countdown, you don't need to ECHO that you're about to delay.

5

Also -- use EXIT at all times, under Windows 7, as just getting to the end of the batch file doesn't necessarily terminate it -- as in earlier Windows versions. Windows 7 may be more sensitive to this that earlier NT versions (e.g. Windows 2000 Professional). This was mentioned in some, but not all of the previous answers.

Details of personal experience to support answer:

After transferring a StarOffice5.2 installation from Windows 2000 to Windows 7, I was getting memory space errors upon terminating the suite. This was not seen in Windows 2000.

Years ago, I had written batch files to automatically back-up and restore soffice.ini, to permit a repair when it becomes corrupted (often enough to be a problem -- the suite fails to load). The automatic backup (triggered by a link to the batch file, placed in Office52\user\config\startup) only occurs about after a 5-second delay, however. I noticed whenever I exited the suite just before the batch file ran, the suite's termination went without error. This pointed me to a problem in the batch files.

After the 'EXIT' Command was placed as the last line on the batch files, the office suite began to terminate without memory-space error messages, at all times, whether or not the Batch Files had run.

2

Once the app is done, it should be exiting. Are you sure the Java app is exiting properly?

2

try:

cmd /c "C:\Program Files (x86)\Java\jre6\bin\javaw.exe" -Xmx1024M -Xms1024M -jar Jilko.jar

2

I was just dealing with the same problem, and it finally resolved itself after making what seemed like random changes to the batchfile- I don't understand why but I'll post it here in case it helps someone else later.

I make use of the SysInternals Pskill utility and the sleep utility since XP Home does not include much in terms of commandline functionality.


This is the batchfile that actualy closes after it's done:

@echo off
start /min C:\Progra~1\PsTools\pskill.exe explorer.exe
start /min C:\Progra~1\PsTools\pskill.exe Powermenu.exe
start /min C:\Progra~1\PsTools\pskill.exe PWGen.exe
start /min C:\Progra~1\PsTools\pskill.exe redshiftgui.exe
start /min C:\Progra~1\PsTools\pskill.exe clipx.exe
sleep 2
start explorer.exe
sleep 3
start C:\Progra~1\ClipX\clipx.exe
sleep 1
start C:\Progra~1\Powermenu\PowerMenu.exe
sleep 1
start /min C:\Progra~1\PWGen\PWGen.exe
sleep 1
start C:\Progra~1\RedshiftGUI\redshiftgui.exe && exit

If I had the last few lines changed like this, the cmd window would stay open until I clicked the 'X' in the corner:

start C:\Progra~1\RedshiftGUI\redshiftgui.exe
sleep 1
start /min C:\Progra~1\PWGen\PWGen.exe && exit

Even when I tried to invoke pskill to kill itself, the cmd.exe process would disappear from Task Manager, and pskill would report from inside it's cmd.exe that the cmd.exe process had been killed, yet the cmd.exe window would still stay up until I clicked the 'X' in the corner:

start C:\Progra~1\RedshiftGUI\redshiftgui.exe
sleep 1
start /min C:\Progra~1\PWGen\PWGen.exe
sleep 1
C:\Progra~1\PsTools\pskill.exe cmd.exe

After I added && exit to every line, I noticed some of them responded to it and would interrupt the batch processing- while others wouldn't.

So I just put one of the responsive ones at the end instead of how I had it originally.

As I said, I don't know why, but am glad this is over with.

2
  • 1
    This is so unnecessary.
    – surfasb
    Jan 26, 2012 at 1:20
  • && exit works for me
    – jekcom
    May 27, 2014 at 11:14
0

Here is how i did it:

  1. Create a batch file with the following contents:

    java -Xms512M -Xmx512M -jar yourFileName.jar -o true
    EXIT
    
  2. Within your exit input stream, add:

    System.exit(1);
    
0

Windows 2003 has no "User Accounts" in the Control Panel by default. I wrote a short batch to open User Accounts:

@echo off  
rundll32.exe %SystemRoot%\system32\netplwiz.dll,UsersRunDll  
exit

It worked OK, the User Accounts opened, but the CMD window remained open as well. After some research here, I added: START "" to the beginning of line 2 like so:

@echo off  
Start "" rundll32.exe %SystemRoot%\system32\netplwiz.dll,UsersRunDll o
exit

Now the User Accounts window opens, remains open and the CMD window closes. Easy Peasy.

0

I have been searching and searching for a solution to close a batch file window when the EXIT command leaves the window open for an unknown reason. I finally stumbled upon a solution.

Remove EXIT from the end of the batch file and use:

Taskkill /IM conhost.exe /F
0

I created a net use batch file on a Windows 7 32-bit machine and the cmd of the batch file won't exit after execution. I run the same batch file on another Windows 7 64-bit machine and the cmd of the batch file exit normally.

I tried Bryan's suggestion and it doesn't work on that Windows 7 32-bit machine because there was no conhost.exe process so I modified it as follows:

Taskkill /IM cmd.exe /F

The net use batch file does not always exit normally and it shows "Terminate batch job (Y/N)" confirmation randomly.

According to this thread I modified the batch file as follows:

@echo off

if "%~1"=="-FIXED_CTRL_C" (
   REM Remove the -FIXED_CTRL_C parameter
   SHIFT
) ELSE (
   REM Run the batch with <NUL and -FIXED_CTRL_C
   CALL <NUL %0 -FIXED_CTRL_C %*
   GOTO :EOF
)

net use \\Server\folder

Taskkill /IM cmd.exe /F

The net use batch file eventually exit normally.

-1

Using exit /b on the last line of the batch script should help.

EXIT command reference - Batch file termination on SS64

4
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Ramhound
    Jan 2, 2022 at 16:21
  • Except that this isn't a "link only" answer.
    – xanda
    Jan 3, 2022 at 17:21
  • You didn't explain the reason exit /b would solve the problem. Anyone can provide a link to ss64.com
    – Ramhound
    Jan 3, 2022 at 18:23
  • Further explanation is not necessary in this case: The OP is able to check the proposal using the builtin help (it's unlikely someone writing batch scripts hasn't heard of the /? facility) as well as their own experimentation. Adding any more would therefore be verbose.
    – xanda
    Jan 3, 2022 at 20:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .