Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
What happens if you use start /wait in front of the commands? –  user3463 Feb 10 '11 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. –  RedGrittyBrick Feb 11 '11 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 '11 at 12:11

3 Answers 3

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:\>
share|improve this answer
    
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 '11 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. –  RedGrittyBrick Feb 11 '11 at 15:43

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.

share|improve this answer

Quick test:

C:\> where java

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.