Can Java program exit code be used to detect disk space exceptions?
My Java program is called from a window script.
Can a Java exit code be used to determine if a Java program was terminated prematurely due to insufficient disk space while it is still loading class files from a JAR file?
I tried an out of memory exception and it returns exit code 1, but return code 0 is returned from disk space. Is this the correct behavior?
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 script package
@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.ClassPaspasibomlApplicationContext] - Update org.springframework.context.support.ClassPaspasibo mlApplicationContext @ 4b222f : display name [org. springframework.context.support.ClassPa thank you mlApplicationContext @ 4b222f]; launch date [Mon. June 29, 18:05:01 SGT 2015]; context hierarchy root [INFO] [2015-06-29 18: 05: 02,050] [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Load XML bean definitions from file [D: \ batch \ dev \ batch_home \ bin \ spring \ ApplicationContext-test.xml]
Delete files over 30 old
del D: \ batch \ dev \ batch_home \ log \ "TEST_20150629_173016.log" Program output 1
Output for disk space: [INFO] [2015-06-29 19: 05: 01.960] [org.springframework.context.support.ClassPa thank you mlApplicationContext] - Update org.springframework.context.support.ClassPa thank you mlApplicationContext @ 4b222f name: .springframework.context.support.ClassPa thank you mlApplicationContext @ 4b222f]; launch date [Mon. June 29, 19:05:01 SGT 2015]; context hierarchy root [INFO] [2015-06-29 19: 05: 02,050] [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Load XML bean definitions from file [D: \ batch \ dev \ batch_home \ bin \ spring \ ApplicationContext-test.xml]
Delete files over 30 old
del D: \ batch \ dev \ batch_home \ log \ "TEST1_20150629_180030.log" Program output 0
source to share
Your batch file is broken. ERRORLEVEL is never empty, it always gets a number in it, unless you are doing something stupid like setting 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%
source to share