gpt4 book ai didi

java - 是否可以使用 Java 程序的退出代码来检测磁盘空间不足异常?

转载 作者:搜寻专家 更新时间:2023-11-01 03:49:55 24 4
gpt4 key购买 nike

我的 Java 程序是从 Windows 脚本调用的。

是否可以使用 Java 退出代码来确定 Java 程序是否在仍在从 JAR 文件加载类文件时由于磁盘空间不足而过早终止?

我尝试了内存不足异常,它返回退出代码 1,但磁盘空间不足返回退出代码 0。这是正确的行为吗?

子类方法:

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;
}

父方法:

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 批处理脚本

@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%

OutOfMemory 的输出:[INFO][2015-06-29 18:05:01,960][org.springframework.context.support.ClassPathXmlApplicationContext] - 刷新 org.springframework.context.support.ClassPathXmlApplicationContext@4b222f:显示名称 [org.springframework.context.support .ClassPathXmlApplicationContext@4b222f];启动日期 [2015 年 6 月 29 日星期一 18:05:01 SGT];上下文层次的根[INFO][2015-06-29 18:05:02,050][org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - 从文件加载 XML bean 定义 [D:\batch\dev\batch_home\bin\spring\applicationContext -test.xml]

删除超过 30 个旧文件

del D:\batch\dev\batch_home\log\"TEST_20150629_173016.log"程序导出 1

磁盘空间不足的输出:[INFO][2015-06-29 19:05:01,960][org.springframework.context.support.ClassPathXmlApplicationContext] - 刷新 org.springframework.context.support.ClassPathXmlApplicationContext@4b222f:显示名称 [org.springframework.context.support .ClassPathXmlApplicationContext@4b222f];启动日期 [2015 年 6 月 29 日星期一 19:05:01 SGT];上下文层次的根[INFO][2015-06-29 19:05:02,050][org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - 从文件加载 XML bean 定义 [D:\batch\dev\batch_home\bin\spring\applicationContext -test.xml]

删除超过 30 个旧文件

del D:\batch\dev\batch_home\log\"TEST1_20150629_180030.log"程序导出0

最佳答案

您的批处理文件已损坏。 ERRORLEVEL 永远不会为空,它总是有一个数字,除非你做了一些愚蠢的事情,比如将它设置为一个字符串。

@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%

关于java - 是否可以使用 Java 程序的退出代码来检测磁盘空间不足异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31376840/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com