1@echo off 2 3rem Based on scalac.bat from the Scala distribution 4rem Copyright 2002-2011, LAMP/EPFL 5rem Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. 6rem Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. 7 8rem We adopt the following conventions: 9rem - System/user environment variables start with a letter 10rem - Local batch variables start with an underscore ('_') 11 12setlocal 13call :set_home 14 15if "%_KOTLIN_COMPILER%"=="" set _KOTLIN_COMPILER=org.jetbrains.kotlin.cli.jvm.K2JVMCompiler 16 17if not "%JAVA_HOME%"=="" ( 18 rem Prepend JAVA_HOME to local PATH to be able to simply execute "java" later in the script. 19 set "PATH=%JAVA_HOME%\bin;%PATH%" 20) 21 22rem We use the value of the JAVA_OPTS environment variable if defined 23if "%JAVA_OPTS%"=="" set JAVA_OPTS=-Xmx256M -Xms128M 24 25rem Iterate through arguments and split them into java and kotlin ones 26:loop 27set _arg=%~1 28if "%_arg%" == "" goto loopend 29 30if "%_arg:~0,2%"=="-J" ( 31 if "%_arg:~2%"=="" ( 32 echo error: empty -J argument 33 goto error 34 ) 35 set JAVA_OPTS=%JAVA_OPTS% "%_arg:~2%" 36) else ( 37 if "%_arg:~0,2%"=="-D" ( 38 set JAVA_OPTS=%JAVA_OPTS% "%_arg%" 39 ) else ( 40 set KOTLIN_OPTS=%KOTLIN_OPTS% "%_arg%" 41 ) 42) 43shift 44goto loop 45:loopend 46 47setlocal EnableDelayedExpansion 48 49call :set_java_version 50if !_java_major_version! geq 9 ( 51 rem Workaround the illegal reflective access warning from ReflectionUtil to ResourceBundle.setParent, see IDEA-248785. 52 set JAVA_OPTS=!JAVA_OPTS! "--add-opens" "java.base/java.util=ALL-UNNAMED" 53) 54 55if "!_KOTLIN_RUNNER!"=="1" ( 56 java !JAVA_OPTS! "-Dkotlin.home=%_KOTLIN_HOME%" -cp "%_KOTLIN_HOME%\lib\kotlin-runner.jar" ^ 57 org.jetbrains.kotlin.runner.Main %KOTLIN_OPTS% 58) else ( 59 set _ADDITIONAL_CLASSPATH= 60 61 if !_java_major_version! lss 13 ( 62 set JAVA_OPTS=!JAVA_OPTS! "-noverify" 63 ) 64 65 if not "%_KOTLIN_TOOL%"=="" ( 66 set _ADDITIONAL_CLASSPATH=;%_KOTLIN_HOME%\lib\%_KOTLIN_TOOL% 67 ) 68 69 java !JAVA_OPTS! -cp "%_KOTLIN_HOME%\lib\kotlin-preloader.jar" ^ 70 org.jetbrains.kotlin.preloading.Preloader -cp "%_KOTLIN_HOME%\lib\kotlin-compiler.jar!_ADDITIONAL_CLASSPATH!" ^ 71 %_KOTLIN_COMPILER% %KOTLIN_OPTS% 72) 73 74goto end 75 76rem ########################################################################## 77rem # subroutines 78 79:set_home 80 set _BIN_DIR= 81 for %%i in (%~sf0) do set _BIN_DIR=%_BIN_DIR%%%~dpsi 82 set _KOTLIN_HOME=%_BIN_DIR%.. 83goto :eof 84 85rem Parses "java -version" output and stores the major version to _java_major_version. 86rem Note that this only loads the first component of the version, so "1.8.0_265" -> "1". 87rem But it's fine because major version is 9 for JDK 9, and so on. 88rem Needs to be executed in the EnableDelayedExpansion mode. 89:set_java_version 90 set _version= 91 rem Parse output and take the third token from the string containing " version ". 92 rem It should be something like "1.8.0_275" or "15.0.1". 93 for /f "tokens=3" %%i in ('java -version 2^>^&1 ^| findstr /i " version "') do ( 94 rem Split the string by "-" or "." and take the first token. 95 for /f "delims=-. tokens=1" %%j in ("%%i") do ( 96 rem At this point, _version should be something like "1 or "15. Note the leading quote. 97 set _version=%%j 98 ) 99 ) 100 if "!_version!"=="" ( 101 rem If failed to parse the output, set the version to 1. 102 set _java_major_version=1 103 ) else ( 104 rem Strip the leading quote. 105 set _java_major_version=!_version:~1! 106 ) 107goto :eof 108 109:error 110set ERRORLEVEL=1 111 112:end 113exit /b %ERRORLEVEL%