f



Exit code of a batch (using exit /B)

Hi, group

java 1.4.2, windows xp professional

With test.bat like this:

@echo Hi
@exit -15

I run it with
String execArgs[]={"test.bat"};
Runtime.getRuntime().exec(execArgs[]);
.... Read OutputStream and close
.... Read ErrorStream and close
int code = p.waitFor();

And the returned code is Ok (-15).

Using

@echo Hi
@exit /B -15

The returned code is always 0.
I've tried using
String execArgs[] = {"cmd","/C","test.bat"};
But the result is the same...

Searched in google and in the newsgroup, but I haven't found the reason yet.

Has anybody come accross the same problem?
The real "batch" file is provided by a third company, I don't have the right
to change it, and it's in the second form (with /B).

Thanks!


0
nospam21 (19047)
4/28/2004 12:05:22 PM
comp.lang.java.programmer 52711 articles. 1 followers. Post Follow

3 Replies
1010 Views

Similar Articles

[PageSpeed] 51

User Joe Smith wrote:
[...]
> Using
> 
> @echo Hi
> @exit /B -15
> 
> The returned code is always 0.
> I've tried using
> String execArgs[] = {"cmd","/C","test.bat"};
> But the result is the same...
[...]

Maybe you should try reading %ERRORLEVEL% variable

Tomek


0
tlas (2)
4/28/2004 1:11:33 PM
Joe Smith wrote:
> Using
> 
> @echo Hi
> @exit /B -15
> 
> The returned code is always 0.

The /B option tells the batch file to only partially exit.  The error 
code is then stored in an environment variable of the surrounding 
command interpreter, which then promptly exits.  In other words, it's 
lost, and there's absolutely no way to retrieve it.

What you could do is wrap this batch file in one of your own, which 
simply calls this one and that does:

    exit %errorlevel%

-- 
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
0
cdsmith (3862)
4/28/2004 1:40:18 PM
> > Using
> >
> > @echo Hi
> > @exit /B -15
> >
> > The returned code is always 0.
>
....
> What you could do is wrap this batch file in one of your own, which
> simply calls this one and that does:
>
>     exit %errorlevel%
>

Hi,
thanks for replying.
I've tried that, creating test2.bat:
@test.bat or @cmd /C test.bat
@echo Exit value: %ERRORLEVEL% (also tried in lower case, just in case)
@exit %ERRORLEVEL%

but it doesn't work...
When using directly @test.bat, the exit /B it contains stops the execution
of test2.bat, so I don't get to see the "Exit value" message.

?f I use @cmd /C test.bat, this creates a new environment for test.bat, and
the %errorlevel% variable is not passed to the environment of test2.bat, so
it's always 0.

I'm afraid this is moving to offtopic, but if you have more ideas... :)

Bye!


0
nospam21 (19047)
4/28/2004 2:08:48 PM
Reply: