“error: can't find main class scala.tools.nsc.MainGenericRunner” when running scala in windows

江枫思渺然 提交于 2021-02-09 12:01:15

问题


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 !


回答1:


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.




回答2:


If you are trying to create an environmental variable that doesn’t let you include spaces in the directory, you need to use Window’s shortened pathname.

Instead of “C:\Program Files\scala…”, use “C:\Progra~1\scala…”.

Instead of “C:\Program Files (x86)\scala…”, use “C:\Progra~2\scala…”.

OTHERWISE,

This guy found the solution: https://issues.scala-lang.org/browse/SI-7821

Solution:

To make the changes he talks about to the batch files, download Notepad++

Then right-click on NotePad++ and select "Run as Administrator" or "Run as PowerBroker Administrator".

Open a new file in NotePad++

Navigate to your scala/bin/scala.bat file directory and select scala.bat

Change the code to what the above link says. So the final result should be this:

Make the changes to :add_cpath and :set_home for all the .bat files inside the scala/bin folder.

Now open command prompt and type "scala -version" or just "scala"




回答3:


I got the same error after I installed Scala in the new volume formatted by Windows 10.

Workaround is to install Scala to the path without spaces. e.g. D:\scala

scala.bat uses 8.3 filenames to set up _SCALA_HOME, but 8.3 filename generation on NTFS volumes can be disabled by option (and it seems default now).

To check whether 8.3 filename generation is enabled, run dir /x and see if short file/folder names are displayed beside long names.

C:\>dir /x
2016/04/19  08:26    <DIR>          PROGRA~1     Program Files
D:\>dir /x
2016/04/19  14:38    <DIR>                       Program Files

Or use fsutil 8dot3name command in the administrator command prompt (Replace D: with your installation drive. TechNet documentation):

fsutil 8dot3name query D:


来源:https://stackoverflow.com/questions/18547257/error-cant-find-main-class-scala-tools-nsc-maingenericrunner-when-running-sc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!