Exit code of a batch (using exit /B)

J

Joe Smith

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!
 
T

Tomek

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
 
C

Chris Smith

Joe said:
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
 
J

Joe Smith

....
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!
 
Joined
Nov 8, 2006
Messages
1
Reaction score
0
Runtime.exec running command line script returns exit code always 0 in Windows XP/2K

I am trying to execute a batch file containing following statements from JAVA program
-------------
@echo Off
EXIT /b 44
--------------

The java program when executed, Runtime.exec() always returns me exit code of 0 where as it should return 44. The same works on windows 2003 which is strange. I havent found a way to get around this problem.

Any help on this is appreciated.

Thanks,
Sandeep
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top