Exit code of a batch (using exit /B)

Discussion in 'Java' started by Joe Smith, Apr 28, 2004.

  1. Joe Smith

    Joe Smith Guest

    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!
    Joe Smith, Apr 28, 2004
    #1
    1. Advertising

  2. Joe Smith

    Tomek Guest

    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
    Tomek, Apr 28, 2004
    #2
    1. Advertising

  3. Joe Smith

    Chris Smith Guest

    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
    Chris Smith, Apr 28, 2004
    #3
  4. Joe Smith

    Joe Smith Guest


    > > 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!
    Joe Smith, Apr 28, 2004
    #4
  5. Joe Smith

    sandeep1976

    Joined:
    Nov 8, 2006
    Messages:
    1
    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
    sandeep1976, Nov 8, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2FuZHk=?=

    Code to Exit Web App and Exit Internet Explorer

    =?Utf-8?B?U2FuZHk=?=, Aug 3, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    7,876
    =?Utf-8?B?U2FuZHk=?=
    Aug 5, 2005
  2. Replies:
    2
    Views:
    443
    Jeff Epler
    May 31, 2005
  3. Yujo
    Replies:
    2
    Views:
    395
    Gabriel Genellina
    Apr 10, 2007
  4. jacob navia
    Replies:
    3
    Views:
    542
    Nick Keighley
    Feb 24, 2010
  5. Keith Thompson
    Replies:
    10
    Views:
    672
    Tim Rentsch
    Mar 3, 2010
Loading...

Share This Page