& ldquo; error: can not find the main class scala.tools.nsc.MainGenericRunner & rdquo; when running scala in Windows

advertisements

after downloaded scala 2.10.2 for windows, and run scala I met such error:

"错误: 找不到或无法加载主类 scala.tools.nsc.MainGenericRunner"

which means "error: can't find or load main class scala.tools.nsc.MainGenericRunner". So I check the scala.bat for reasons, and I found such function:

:set_home
  set _BIN_DIR=
  for %%i in (%~sf0) do set _BIN_DIR=%_BIN_DIR%%%~dpsi
  echo in set_home: %_BIN_DIR%
  set _SCALA_HOME=%_BIN_DIR%..
goto :eof

After this function the _SCALA_HOME become D:\program files\scala\files\scala\bin\.., which is obviously wrong. Anyway, after setting _SCALA_HOME to be right path the error fixed. However, do any one knows what %~sf0 and %%~dpsi mean and what this function is really trying to do ? Thank you!


thank you @gourlaysama

I finally found the real reason:execute the following code and you can see the result is :

  for %%i in (%~sf0) do (
    echo "%%"i is: %%i
    echo sf0 is : %%~dpsi
    set _BIN_DIR=%_BIN_DIR%%%~dpsi
    )

output:

"%"i is: D:\program
sf0 is : D:\
"%"i is: files\scala\bin\scala.bat
sf0 is : D:\program files\scala\bin\files\scala\bin\

so the malfunction is result from the extra space between program files !


Those weird variables are called parameter extensions. they allow you to interpret a variable as a path to a file/directory and directly resolve things from that path.

For example, if %1 is a path to a file dir123456\file.txt,

  • %~f1 is the fully qualified path to file.txt,
  • %~p1 is the path to the containing directory dir123456,
  • %~s1 is the path in short name format dir123~1\file.txt,
  • and many others...

Also, %0 is always set to the path of the currently running script. So:

  • %~fs0 is the fully qualified path, in short name format, to the current script,
  • %%~dpsi is the manual expansion of the FOR variable %%i to a drive letter (d option) followed by the path the containing folder (p option), in short format (s option).

Now, this weird looking block of code is a workaround for KB833431 in which the %~dps0 command does not give you the path to the folder of the current script (in short format) although it should. This was fixed in XP SP2.

It seems to be reconstructing manually the fully qualified path to the scala bin directory from the fully qualified path to scala.bat, and then just getting that directory's parent, which should be a valid _SCALA_HOME.