4

My Java program is called from a windows script.

Is it possible to use the Java exit code to determine whether the Java program was terminated prematurely due to out of disk space while it is still loading the class files from the JAR file?

I tried out of memory exception and it returns an exit code 1 but out of disk space returns exit code 0. Is this correct behaviour?

Child class method:

public int executeBatch() {
        logger.info("executeBatch() - Send Email Alert Start");
        try {
            alertTransactionMgr.sendEmailAlert();
        } catch (Exception e) {
            throw new Exception(e);
        }
        logger.info("executeBatch() - Send Email Alert End");
        return 0;
    } 

Parent method:

public int execute() {

        this.trx = createTransaction();

        try {
            returnCode = executeBatch();

        } catch (Exception e) {
            printLogErrorMsg("Job Failed caused by the Exception.", e);
            returnCode = -1;
            trx.setStatus("Failure");
            updateBatchTransaction(trx);

        }
        return returnCode;
    }

Windows batch script

@echo off

set ERRLVL=0

java -cp %CLASSPATH% com.test.runner.MainBatchRunner
if not (%ERRORLEVEL%)==() (
    set ERRLVL=%ERRORLEVEL%
)

echo Delete Files that are more than 30 old
forfiles /p "%BATCH_LOG_DIR%" /s /m %2*.log /d -%ARCHIVE_DAYS% /c "cmd /c echo del %BATCH_LOG_DIR%\@file"
forfiles /p "%BATCH_LOG_DIR%" /s /m %2*.log /d -%ARCHIVE_DAYS% /c "cmd /c del %BATCH_LOG_DIR%\@file"

echo Program exit %ERRLVL%
echo Program exit %ERRLVL% >> %BATCH_LOG_FILE%

exit /B %ERRLVL%

Output for OutOfMemory: [INFO ][2015-06-29 18:05:01,960][org.springframework.context.support.ClassPathXmlApplicationContext] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4b222f: display name [org.springframework.context.support.ClassPathXmlApplicationContext@4b222f]; startup date [Mon Jun 29 18:05:01 SGT 2015]; root of context hierarchy [INFO ][2015-06-29 18:05:02,050][org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from file [D:\batch\dev\batch_home\bin\spring\applicationContext-test.xml]

Delete Files that are more than 30 old

del D:\batch\dev\batch_home\log\"TEST_20150629_173016.log" Program exit 1

Output for Out of disk space: [INFO ][2015-06-29 19:05:01,960][org.springframework.context.support.ClassPathXmlApplicationContext] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4b222f: display name [org.springframework.context.support.ClassPathXmlApplicationContext@4b222f]; startup date [Mon Jun 29 19:05:01 SGT 2015]; root of context hierarchy [INFO ][2015-06-29 19:05:02,050][org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from file [D:\batch\dev\batch_home\bin\spring\applicationContext-test.xml]

Delete Files that are more than 30 old

del D:\batch\dev\batch_home\log\"TEST1_20150629_180030.log" Program exit 0

javanoobie
  • 41
  • 4
  • Please show the code and output demonstrating the problem; an exit code of 0 if a program fails to load at all is highly suspicious. Also of note is that any exception rethrown from the main thread causes the JVM to exit with 1, so this is not really a meaningful exit code. – fge Jul 13 '15 at 06:54
  • I agree with @fge. An `OutOfMemoryException` should return a non-zero exit code. – M. Shaw Jul 13 '15 at 07:05
  • Is `OutOfMemory` not related to working memory, instead of disk space? – Timo Jul 13 '15 at 09:55
  • Hi...The error I hit is out of disk space for log file spooling. So can I say that the program did execute but because of not enough disk space, it fails and program exits with 0 – javanoobie Jul 14 '15 at 08:50
  • The "ERRORLEVEL" you get from running a program from batch/commandline is determined by the process exit code - set via System.exit(), see: http://stackoverflow.com/questions/2441036/return-type-of-main-in-java. You should catch whatever exceptions you care about and call System.exit(n) where "n" is a number that the batch script can then check for via %ERRORLEVEL% – joeking Jul 29 '15 at 00:09
  • Im bumping this because i feel that knowing how to call exit codes of programs would be significantly useful to my own personal projects, but have yet to find documentation on such information. Generally i feel like the exit codes are solely explicit to the language but I feel that theres a part of the computer that identifies when it exits regardless of what program that is. – Jouster500 Mar 20 '16 at 02:00

2 Answers2

0

Your batch file is broken. ERRORLEVEL is never empty, it's always got a number in it, unless you do something stupid like set it to a string.

@echo off

set ERRLVL=0

java -cp %CLASSPATH% com.test.runner.MainBatchRunner
@rem if not (%ERRORLEVEL%)==() (
@rem    set ERRLVL=%ERRORLEVEL%
@rem)
if %ERRORLEVEL% != 0 set ERRLVL=%ERRORLEVEL%

echo Delete Files that are more than 30 old
forfiles /p "%BATCH_LOG_DIR%" /s /m %2*.log /d -%ARCHIVE_DAYS% /c "cmd /c echo del %BATCH_LOG_DIR%\@file"
forfiles /p "%BATCH_LOG_DIR%" /s /m %2*.log /d -%ARCHIVE_DAYS% /c "cmd /c del %BATCH_LOG_DIR%\@file"

echo Program exit %ERRLVL%
echo Program exit %ERRLVL% >> %BATCH_LOG_FILE%

exit /B %ERRLVL%
jwdonahue
  • 6,199
  • 2
  • 21
  • 43
0

I am wondering why your java application should break because of full disk space. You mention the startup of the JVM, which is more or less about reading files into RAM. The only disk space involved here could be virtual memory if computer is already swapping heavily - this will lead to an OutOfMemoryException, but never to a disk space issue.

Once your application is loaded it can check the filesystem roots and decide how much disk space is left. If not sufficient, it can then terminate with system.exit(int) and give a meaningful exit code of your choice to the OS or batch file.

0 means the application ran ok 1-127 means whatever you define 128- are usually OS errors, like 'could not find executable' or 'permission denied'...

Queeg
  • 7,748
  • 1
  • 16
  • 42