Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SBT fails with `String.class is broken`

Tags:

scala

sbt

sbt fails with a cryptic error on issuing any command (compile, assembly, clean or any other).

$ sbt --version                                                                                                                                                          
error: error while loading String, class file '/modules/java.base/java/lang/String.class' is broken
(class java.lang.NullPointerException/null)

I am on a machine running macOS, and sbt was installed via homebrew. I have tried upgrading to the latest versions of sbt (1.3.10), but the error still persists.

like image 270
suj1th Avatar asked Apr 17 '20 11:04

suj1th


4 Answers

The issue is documented on the SBT Download page.

Homebrew maintainers have added a dependency to JDK 13 because they want to use more brew dependencies (brew#50649). This causes sbt to use JDK 13 even when java available on PATH is JDK 8 or 11. To prevent sbt from running on JDK 13, install jEnv or switch to using SDKMAN.

I was able to resolve the problem by using JDK 8 via jEnv.

like image 112
suj1th Avatar answered Nov 14 '22 10:11

suj1th


Since sbt documents JDK 8 and 11 as compatible versions

We recommend AdoptOpenJDK JDK 8 or AdoptOpenJDK JDK 11

try controlling which JDK is used by sbt via -java-home setting which can be configured system-wide via sbtopts run configuration

/usr/local/etc/sbtopts

or per-project basis via


<project-root>/.sbtopts

For example, to configure JDK used by sbt in current project, try setting in .sbtopts

-java-home /Users/picard/.sdkman/candidates/java/current
like image 14
Mario Galic Avatar answered Nov 14 '22 10:11

Mario Galic


This is what solved my problem on my Mac.

brew uninstall sbt

Install sdkman

curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"

Install sbt via sdk

sdk install sbt
like image 7
yantaq Avatar answered Nov 14 '22 11:11

yantaq


I had the same issue recently. What worked for me is to install SDKMAN(https://sdkman.io/)

$ curl -s "https://get.sdkman.io" | bash
...
$ source "$HOME/.sdkman/bin/sdkman-init.sh"
...

After installation, I wanted to see what versions of Java I can install. So I simply run this command to list all the available versions of Java

sdk list java

Choose the version you want to install (recommend installing either 8 or 11 as mentioned above) and simply run the command with the identifier that specifies your version from the list

sdk install java 11.0.3.hs-adpt

After installation, it set the Java 11 to default on my system. I then ran the sbt command again and it worked.

like image 6
Mantas Astra Avatar answered Nov 14 '22 10:11

Mantas Astra