3

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 !

Flybywind
  • 968
  • 1
  • 11
  • 23
  • also, did you use the MSI or ZIP installer? and where did you extract it? The ZIP file does not contain a `files` subdirectory but directly `bin` and others. – gourlaysama Aug 31 '13 at 14:06
  • I tried MSI and ZIP, and both there not exist `file` directory – Flybywind Sep 01 '13 at 02:47
  • thanks for the edit, it is clearer now. It could be a bug with ~s (there are several that were never fixed...) under cmd.exe. Which version of Windows are you using? – gourlaysama Sep 01 '13 at 17:29
  • I had the same error. All I needed is change SCALA_HOME to correct one. For me this is "C:\Program Files\scala" not a "C:\Program Files\scala\bin" – kisileno Sep 03 '13 at 14:25
  • @gourlaysama sorry to reply late, I use win7 Pro SP1 – Flybywind Sep 07 '13 at 14:23

3 Answers3

2

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.

gourlaysama
  • 11,240
  • 3
  • 44
  • 51
1

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: enter image description here

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".

enter image description here

Open a new file in NotePad++

enter image description here

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

enter image description here

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

enter image description here

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"

Gene
  • 10,819
  • 1
  • 66
  • 58
0

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:
shiratori
  • 61
  • 1
  • 3