簡體   English   中英

在 Windows 中批量編譯 Kotlin 源代碼時出錯

[英]Error on batch compiling Kotlin source code in Windows

Microsoft Windows 7我在C:\\new目錄中有兩個 Kotlin 源代碼:

  • 你好1.kt
  • 你好2.kt

我可以在命令行中單獨編譯它們,其當前工作目錄是C:\\ ,例如>kotlinc hello1.kt 但是當我嘗試進行批處理編譯時,出現錯誤:

C:\new>kotlinc *.kt
error: source file or directory not found: *.kt

筆記:

  • kotlinc .\\*.kt結果是一樣的。
  • > dir *.kt正確列出了.kt文件,因此問題不可能出在 about *通配符中。
  • $ kotlinc *.ktLinuxLinux沒有錯誤。

我想知道是什么導致了這個問題? 有沒有辦法在Microsoft Windows進行批處理編譯?

看起來kotlinc.exe不支持通配符模式作為參數。 所以必須在 Windows 上使用kotlinc hello1.kt hello2.kt

在執行可執行文件之前,Linux/Mac 上的 shell 解釋器將諸如*.kt類的通配符模式擴展為具有匹配文件/文件夾名稱的參數字符串列表。 因此,Linux上的外殼口譯/ Mac的不叫kotlinc*.kt ,但稱與可執行hello1.kt hello2.kt

Windows 命令處理器cmd.exe在可執行文件的參數列表中不提供這種通配符擴展。 可執行文件本身必須支持帶有通配符模式的參數,並自行搜索匹配的文件/文件夾。

Windows 批處理文件解決方案將遵循適用於所有文件名的代碼,但帶有感嘆號的文件名除外:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
rem Make the directory of the batch file the current directory.
pushd "%~dp0" || goto :EOF
if exist *.kt goto CreateFilesList
echo ERROR: There is no *.kt file in folder: "%~dp0"
echo/
pause
goto EndBatch

:CreateFilesList
set "FilesList="
for %%I in (*.kt) do set FilesList=!FilesList! "%%I"
rem The files list starts with a space character.
kotlinc.exe!FilesList!
if errorlevel 1 echo/& pause

:EndBatch
rem Restore the initial current directory.
popd
endlocal

一種較慢的解決方案也適用於具有一個或多個.kt文件名的! 在文件名中。

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Make the directory of the batch file the current directory.
pushd "%~dp0" || goto :EOF
if exist *.kt goto CreateFilesList
echo ERROR: There is no *.kt file in folder: "%~dp0"
echo/
pause
goto EndBatch

:CreateFilesList
set "FilesList="
for %%I in (*.kt) do call :AddToList "%%I"
goto RunKotlinC

:AddToList
set FilesList=%FilesList% %1
goto :EOF

:RunKotlinC
rem The files list starts with a space character.
kotlinc.exe%FilesList%
if errorlevel 1 echo/& pause

:EndBatch
rem Restore the initial current directory.
popd
endlocal

請注意,文件名列表不是無限的。 環境變量定義的最大長度為 8192 個字符,其中包括變量名稱、等號、分配給環境變量的字符串值和終止空字節。 添加到列表中的文件名沒有路徑。 因此,只要一次執行kotlinc.exe不應該編譯數百個.kt文件,這個限制在這里應該沒有問題。

要了解使用的命令及其工作原理,請打開命令提示符窗口,在那里執行以下命令,並仔細閱讀為每個命令顯示的所有幫助頁面。

  • call /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • popd /?
  • pushd /?
  • rem /?
  • set /?
  • setlocal /?

也可以看看:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM