Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Windows 綜合教學 >> 關於 Windows

Windows 批次檔中正確呼叫 Groovy 腳本:避免 bat 過早結束

編輯:關於 Windows

在 Windows 批次檔中執行 Groovy 程式碼相當方便,例如取得 UUID:

groovy -e "println(UUID.randomUUID().toString())"
echo "groovy execution finished!"

但若安裝的是 Zip 版 Groovy(非 Windows Installer 版),直接呼叫會導致後續指令不執行、批次檔直接結束。原因在於 Zip 版提供的是 groovy.bat,若不使用 call,控制權會轉移至該批次檔,無法返回。

解決方案有二:

  1. 安裝 Windows Installer 版 Groovy,擁有原生啟動器 groovy.exe。
  2. 在批次檔中使用 call 呼叫:
call groovy -e "println(UUID.randomUUID().toString())"
echo "groovy execution finished!"

加上 call 即可確保控制權返回,後續指令正常執行。

Copyright © Windows教程網 All Rights Reserved