0

When I run Java from a Windows .cmd file (Vista 32-bit here), the Java command causes the batch file to stop executing additional commands.

For example, this is a simple test.cmd file:

java
java

This should cause Java to print its help message twice. However when I run it in cmd.exe, I get this:

C:\>test

C:\>java
Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)

where options include:
    -client       to select the "client" VM
    -server       to select the "server" VM
...

C:\>

The batch file simply exits when Java exits. What's going on here?

3
  • What happens if you use start /wait in front of the commands?
    – user3463
    Feb 10, 2011 at 22:57
  • Your test.cmd works as expected on my PC under Windows XP. If you have both a test.bat and a test.cmd then typing test will run test.bat not test.cmd - try using the command test.cmd instead. Feb 11, 2011 at 10:57
  • just tried it with a.bat or a.cmd and the lines java<enter>java And it worked fine for me.. The only similar situation to yours that i'm aware of, is I have seen .bat files that when you call a bat file from them and they don't come back!! so you have to use call blah.bat within any bat file, then it comes back! But this is not your situation, so no idea what's happening. Unless perhaps you have a java.bat ?! which would be really odd, or maybe cmd, also extremely odd if that were the case. Try changing where it says java in your bat file, to java.exe
    – barlop
    Feb 11, 2011 at 12:11

3 Answers 3

3

Another possibility is that there's a batchfile called java which is getting called, rather than the normal java.exe (though the batch file may invoke the executable, possibly also setting up things like the CLASSPATH beforehand). Executing a batchfile from another will transfer execution to the other batchfile entirely, not returning to the invoker. To execute it and then continue execution of the original batch file, use call:

call java

And see if that makes a difference.

0

If you have both a test.bat and a test.cmd then typing test will run test.bat not test.cmd - try using the command test.cmd instead.


C:\>type test.bat
@echo off
echo a =============================================
java
echo b =============================================

C:\>type test.cmd
echo 1 -------------------------------------------------
java
echo 2 -------------------------------------------------
java
echo 3 -------------------------------------------------

C:\>test

a =============================================

Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)

where options include:
    -client       to select the "client" VM
    -server       to select the "server" VM
    -hotspot      is a synonym for the "client" VM  [deprecated]
                  The default VM is client.

...

b =============================================

C:\>
2
  • Is there any difference between the bat and the cmd in the way they operate after running the first "java"? I don't notice any.
    – barlop
    Feb 11, 2011 at 12:09
  • I see no difference between the way .cmd and .bat operate in this instance. My hunch was merely that the OP might have two executables called test in their %PATH% and be unaware that the one being executed was not the one intended. Feb 11, 2011 at 15:43
0

Quick test:

C:\> where java

With that, you can find out exactly what is being executed.

You must log in to answer this question.

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