java cmd退出_如何从Windows命令行获取应用程序退出代码?

我正在运行一个程序,想查看它的返回代码是什么(因为它会根据不同的错误返回不同的代码)。

我知道在Bash中,我可以通过运行

回声$?

在Windows上使用cmd.exe怎么办?

#1楼

测试ErrorLevel适用于控制台应用程序,但是dmihailescu暗示,如果您试图从命令提示符下运行窗口化应用程序(例如基于Win32的窗口) ,则此方法将无效。 一个窗口化的应用程序将在后台运行,并且控件将立即返回到命令提示符(最有可能的ErrorLevel为零,以指示该进程已成功创建 )。 当窗口应用程序最终退出时,其退出状态将丢失。

但是,与其使用其他地方提到的基于控制台的C ++启动器,一种更简单的选择是使用命令提示符下的START /WAIT命令来启动窗口应用程序。 这将启动带窗口的应用程序,等待其退出,然后将控制权返回到命令提示符,并在ErrorLevel中设置进程的退出状态。

start /wait something.exe

echo %errorlevel%

#2楼

如果要完全匹配错误代码(例如等于0),请使用以下代码:

@echo off

my_nify_exe.exe

if %ERRORLEVEL% EQU 0 (

echo Success

) else (

echo Failure Reason Given is %errorlevel%

exit /b %errorlevel%

)

if errorlevel 0与错误errorlevel > = if errorlevel 0相匹配if /? 。

#3楼

一方面,我需要将Cygwin的日志事件准确地推送到Windows Event日志。 我希望WEVL中的消息是自定义的,具有正确的退出代码,详细信息,优先级,消息等。因此,我创建了一个小Bash脚本来解决此问题。 它在GitHub logit.sh上 。

一些摘录:

usage: logit.sh [-h] [-p] [-i=n] [-s]

example: logit.sh -p error -i 501 -s myscript.sh "failed to run the mount command"

这是临时文件内容部分:

LGT_TEMP_FILE="$(mktemp --suffix .cmd)"

cat<$LGT_TEMP_FILE

@echo off

set LGT_EXITCODE="$LGT_ID"

exit /b %LGT_ID%

EOF

unix2dos "$LGT_TEMP_FILE"

这是在WEVL中创建事件的功能:

__create_event () {

local cmd="eventcreate /ID $LGT_ID /L Application /SO $LGT_SOURCE /T $LGT_PRIORITY /D "

if [[ "$1" == *';'* ]]; then

local IFS=';'

for i in "$1"; do

$cmd "$i" &>/dev/null

done

else

$cmd "$LGT_DESC" &>/dev/null

fi

}

执行批处理脚本并调用__create_event:

cmd /c "$(cygpath -wa "$LGT_TEMP_FILE")"

__create_event

#4楼

当使用未附加到控制台的程序时,它可能无法正常工作,因为当您认为自己具有退出代码时,该应用程序可能仍在运行。 使用C ++的解决方案如下所示:

#include "stdafx.h"

#include "windows.h"

#include "stdio.h"

#include "tchar.h"

#include "stdio.h"

#include "shellapi.h"

int _tmain( int argc, TCHAR *argv[] )

{

CString cmdline(GetCommandLineW());

cmdline.TrimLeft('\"');

CString self(argv[0]);

self.Trim('\"');

CString args = cmdline.Mid(self.GetLength()+1);

args.TrimLeft(_T("\" "));

printf("Arguments passed: '%ws'\n",args);

STARTUPINFO si;

PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof(si) );

si.cb = sizeof(si);

ZeroMemory( &pi, sizeof(pi) );

if( argc < 2 )

{

printf("Usage: %s arg1,arg2....\n", argv[0]);

return -1;

}

CString strCmd(args);

// Start the child process.

if( !CreateProcess( NULL, // No module name (use command line)

(LPTSTR)(strCmd.GetString()), // Command line

NULL, // Process handle not inheritable

NULL, // Thread handle not inheritable

FALSE, // Set handle inheritance to FALSE

0, // No creation flags

NULL, // Use parent's environment block

NULL, // Use parent's starting directory

&si, // Pointer to STARTUPINFO structure

&pi ) // Pointer to PROCESS_INFORMATION structure

)

{

printf( "CreateProcess failed (%d)\n", GetLastError() );

return GetLastError();

}

else

printf( "Waiting for \"%ws\" to exit.....\n", strCmd );

// Wait until child process exits.

WaitForSingleObject( pi.hProcess, INFINITE );

int result = -1;

if(!GetExitCodeProcess(pi.hProcess,(LPDWORD)&result))

{

printf("GetExitCodeProcess() failed (%d)\n", GetLastError() );

}

else

printf("The exit code for '%ws' is %d\n",(LPTSTR)(strCmd.GetString()), result );

// Close process and thread handles.

CloseHandle( pi.hProcess );

CloseHandle( pi.hThread );

return result;

}

#5楼

名为errorlevel伪环境变量存储退出代码:

echo Exit Code is %errorlevel%

另外, if命令具有特殊的语法:

if errorlevel

看if /? 有关详细信息。

@echo off

my_nify_exe.exe

if errorlevel 1 (

echo Failure Reason Given is %errorlevel%

exit /b %errorlevel%

)

警告:如果设置环境变量名称errorlevel ,则%errorlevel%将返回该值,而不是退出代码。 使用( set errorlevel= )清除环境变量,从而允许通过%errorlevel%环境变量访问errorlevel的真实值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值