scala: Error occurred during initialization of VM

2020-07-27 03:35发布

问题:

I tried to work with easy example of scala language. I installed sbt successfully. When run sbt -h I see help message from sbt:

Usage: sbt [options]

  -h | -help         print this message
  -v | -verbose      this runner is chattier
  -d | -debug        set sbt log level to debug
  -no-colors         disable ANSI color codes
  -sbt-create        start sbt even if current directory contains no sbt project
  -sbt-dir   <path>  path to global settings/plugins directory (default: ~/.sbt)
  -sbt-boot  <path>  path to shared boot directory (default: ~/.sbt/boot in 0.11 series)
  -ivy       <path>  path to local Ivy repository (default: ~/.ivy2)
  -mem    <integer>  set memory options (default: 1536, which is -Xms1536m -Xmx1536m -XX:MaxPermSize=384m -XX:ReservedCodeCacheSize=192m) ....

but when I tried to run in the directory with scala assignments (it's Coursera course) I caught error:

nazar_art@nazar-desctop:~/scala/example$ sbt
Detected sbt version 0.12.1
Starting sbt: invoke with -help for other options
Error occurred during initialization of VM
Could not reserve enough space for object heap

but it should open the sbt command prompt. Smt like this:

shell$ cd /path/to/progfun-project-directory                        # This is the shell of the operating system
shell$ sbt
> _                                                                 # This is the sbt shell

UPDATE:

After I add this following line

java -Xms64M -Xmx256M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=64M -jardirname $0/sbt-launch.jar "$@"

at /usr/bin I see next output:

nazar_art@nazar-desctop:/usr/bin$ sbt
bash: /usr/bin/sbt: Permission denied
nazar_art@nazar-desctop:/usr/bin$ sudo sbt
sudo: sbt: command not found

- Why this happen?
- How to solve this issue?

回答1:

First of all, The example folder name (and the folder name that you have installed sbt) cannot include "!". this probably applies to other special characters as well. Even, I recommend to install SBT in a folder that has no space on its path (and the same thing for examples folder).

For example, if you have installed SBT (or examples folder) on a path like:

/Users/dashti/My Programs/sbt-0.12.1

You can re-install it a path like:

/Users/dashti/MyPrograms/sbt-0.12.1

By applying this naming change, if the error is gone, please ignore the rest of this post.

Otherwise, to fix the mentioned error, first find sbt executable script, then edit it and correct any problem in it:

If you are using a UNIX-based operating system, please follow these steps:

1- Find SBT: Open a terminal and type this command:

which sbt

and you will receive a path like:

/Users/dashti/Documents/scala-2.10.0/bin/sbt

and in this file, there is a command for executing a java program, like:

java -Xms512M -Xmx1024M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=128M -jar `dirname $0`/sbt-launch.jar "$@"

2- Edit sbt script: by entering this command in terminal:

vim /Users/dashti/Documents/scala-2.10.0/bin/sbt

you will be able to edit this file and maybe you should change the memory usage arguments to a lower specification ( You should think about whether you have enough RAM to be running the sbt). For example, this one will be a better choice:

java -Xms64M -Xmx256M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=64M -jar `dirname $0`/sbt-launch.jar "$@"


回答2:

SBT needs more memory allocated to the JVM.

This can be done in build.sbt:

javaOptions ++= Seq(
    "-Xms64m",
    "-Xmx256M",
    "-XX:+CMSClassUnloadingEnabled",
    "-XX:MaxPermSize=64M"
);

Creating an sbt file(no extentions) and running ./sbt is better, but with the code below(@MohammadDashti's suggestion), sbt-launch.jar needs to be in the project folder.

java -Xms512M -Xmx1024M -Xss1M -XX:+CMSClassUnloadingEnabled \
-XX:MaxPermSize=128M -jar `dirname $0`/sbt-launch.jar "$@"

Later you could add javaagent:/path/to/jrebel -Drebel.remoting_plugin=true and re-deploy changes on save with a free JRebel for Scala license.