SBT fails with `String.class is broken` - scala

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.

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.

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

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

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.

If you are using SDKMAN then in some cases certain versions of Java (i.e. JDK) from different vendor might help.
E.g. I was using Amazon Correto JDK8 (version 8.332.08.1-amzn) and it could not properly build my sbt project in IntellIJ IDEA, so I've switched to using Zulu's JDK8 (version 8.0.332-zulu) as default java version.
Hope this helps and good luck. :)

Related

Cannot launch CDT: java.lang.ClassNotFoundException: org.eclipse.core.runtime.adaptor.EclipseStarter

I am trying to use Eclipse CDT under Ubuntu 18.04 LTS.
I get the same error as many others, but I could not find a solution in what I read.
I try to launch with
$ eclipse &
OpenJDK 64-Bit Server VM warning: Ignoring option MaxPermSize; support was removed in 8.0
and I get
/home/user1/.eclipse/org.eclipse.platform_3.8_155965261/configuration/1551271296090.log
When checking /usr/lib/eclipse/configuration/config.ini (as per this) I found the following lines (among others)
osgi.framework=file\:plugins/org.eclipse.osgi_3.8.1.dist.jar
osgi.bundles=reference\:file\:org.eclipse.equinox.simpleconfigurator_1.0.301.dist.jar#1\:start
org.eclipse.equinox.simpleconfigurator.configUrl=file\:org.eclipse.equinox.simpleconfigurator/bundles.info
As for the first two lines, I have files
$ locate eclipse.osgi_
/usr/share/java/org.eclipse.osgi_3.8.1.dist.jar
$ locate simpleconfigurator_1
/usr/lib/eclipse/plugins/org.eclipse.equinox.simpleconfigurator_1.0.301.dist.jar
Nevertheless:
/usr/share/java/org.eclipse.osgi_3.8.1.dist.jar seems to belong to no package (a remnant of some old package?), since
$ apt-file search /usr/share/java/org.eclipse.osgi_3.8.1.dist.jar
gives no results.
I have ver 3.9.1
$ dpkg -l | grep libequinox-osgi-java
ii libequinox-osgi-java 3.9.1-1 all Equinox OSGi framework
$ dpkg -L libequinox-osgi-java
/.
/usr
/usr/share
/usr/share/doc
/usr/share/doc/libequinox-osgi-java
/usr/share/doc/libequinox-osgi-java/changelog.Debian.gz
/usr/share/doc/libequinox-osgi-java/copyright
/usr/share/java
/usr/share/java/org.eclipse.osgi-3.9.1.jar
/usr/share/maven-repo
/usr/share/maven-repo/org
/usr/share/maven-repo/org/eclipse
/usr/share/maven-repo/org/eclipse/osgi
/usr/share/maven-repo/org/eclipse/osgi/org.eclipse.osgi
/usr/share/maven-repo/org/eclipse/osgi/org.eclipse.osgi/3.9.1
/usr/share/maven-repo/org/eclipse/osgi/org.eclipse.osgi/3.9.1/org.eclipse.osgi-3.9.1.pom
/usr/share/maven-repo/org/eclipse/osgi/org.eclipse.osgi/debian
/usr/share/maven-repo/org/eclipse/osgi/org.eclipse.osgi/debian/org.eclipse.osgi-debian.pom
/usr/share/java/org.eclipse.osgi.jar
/usr/share/maven-repo/org/eclipse/osgi/org.eclipse.osgi/3.9.1/org.eclipse.osgi-3.9.1.jar
/usr/share/maven-repo/org/eclipse/osgi/org.eclipse.osgi/debian/org.eclipse.osgi-debian.jar
So I do not know if the problem is here.
How can I solve this?
Could not find an answer here
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=891956
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=898086
https://bugs.launchpad.net/ubuntu/+source/eclipse/+bug/1754886
https://ubuntu.pkgs.org/16.04/ubuntu-universe-i386/libequinox-osgi-java_3.8.1-8_all.deb.html
https://askubuntu.com/questions/1031171/eclipse-doesnt-start-on-ubuntu-18-04
You are probably using an older Eclipse version that does not work with Java 9 or higher:
If using Java 9 or newer please use Eclipse 4.7.1a or newer as it contains fixes in Eclipse launcher to add all JVM modules.
Do one of the following to solve the problem:
Use Java 8 to run Eclipse (a JRE/JDK can be put in the subfolder jre of your Eclipse installation or be specified in the eclipse.ini file)
Upgrade Eclipse (recommended).

Scala sbt run - "Unsupported major.minor version 52.0"

Things run without any problem when I work on IntelliJ, but when I try to compile and run my Scala code in the command line (with sbt run) the following error message pops up.
[error] (run-main-0) java.lang.UnsupportedClassVersionError: akka/event/LogSource : Unsupported major.minor version 52.0
java.lang.UnsupportedClassVersionError: akka/event/LogSource : Unsupported major.minor version 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:803)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2625)
at java.lang.Class.getMethod0(Class.java:2866)
at java.lang.Class.getMethod(Class.java:1676)
I'm using Ubuntu 14.04 LTS.
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty
And my Scala version is 2.9.2.
$ scalac -version
Scala compiler version 2.9.2 -- Copyright 2002-2011, LAMP/EPFL
I barely started learning Scala a few weeks ago, so if anyone could provide some advice on what I should look into, it would be greatly appreciated.
It turned out that there was a mismatch in the JRE version. In the IntelliJ project settings the project SDK was set to be Java 1.8 (on my local machine), whereas JRE 1.7 was installed on the Ubuntu system (an EC2 instance).
In Ubuntu 14.04, the OpenJDK 1.8 is not available by default so the following repository must be added manually. Then the OpenJDK can be installed.
sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update
sudo apt-get install openjdk-8-jre
The default Java version in the system can be changed by running the following command.
sudo update-alternatives --config java
Once I installed openjdk-8-jre and adjusted the default Java version in the system, the sbt run command ran without any issue.
I am using a Mac and trying to learn Scala by following a simple Scala tutorial. I got this error when I am trying to run the app.
Reason:
My installed java version was 8. But on my Terminal the version I was using was 7.
Fix:
I updated the java version to the newest version and then it worked.
Update to correct java version and compile again. It should work fine
I encountered the same proble with CentOS-7.6, here is my solution:
run vim /etc/profile in command line, check your Environment variable to find the jdk version, change it to jdk-1.8. Save and quit safely.
example picture
run /usr/sbin/alternatives --config java, choose version-1.8.
run /usr/sbin/alternatives --config javac, also choose version-1.8.
run source /etc/profile to make the settings work.
By doing the four steps, I was able to run command sbt again without orror.sbt picture
I suddenly started getting this error on Windows 10. Turned out my path had somehow reverted to point to an old version of the java jdk. To be thorough, I first upgraded to the latest jdk (from here for Oracle), which for me was 8u152. I then corrected my path entry to
c:\Program Files\Java\jdk1.8.0_152\bin
(This would be a little different for the 32 bit version.) I then verified that javac -version indeed returned 1.8.0_152 (make sure to open a fresh terminal - an already open one will remember the old path).
And then sbt run worked. (Again remembering to do this from a fresh terminal so path is fresh.)

Failed to initialize compiler: object java.lang.Object in compiler mirror not found

On Ubuntu 16.04, I installed scala:
$ls ~/Binary/scala-2.11.8
bin doc lib man
$grep -A 2 SCALA ~/.bashrc
SCALA=~/Binary/scala-2.11.8
SBT=~/Binary/sbt
export PATH=$PATH:$SCALA/bin:$SBT/bin
With openjdk8 installed, scala fails when opening the REPL:
$javac -version
javac 1.8.0_91
$
$scala
Welcome to Scala 2.11.8 (OpenJDK 64-Bit Server VM, Java 9-internal).
Type in expressions for evaluation. Or try :help.
scala>
Failed to initialize compiler: object java.lang.Object in compiler mirror not found.
** Note that as of 2.8 scala does not assume use of the java classpath.
** For the old behavior pass -usejavacp to scala, or if using a Settings
** object programmatically, settings.usejavacp.value = true.
Failed to initialize compiler: object java.lang.Object in compiler mirror not found.
** Note that as of 2.8 scala does not assume use of the java classpath.
** For the old behavior pass -usejavacp to scala, or if using a Settings
** object programmatically, settings.usejavacp.value = true.
How can I resolve it?
Using the Java 8 version of OpenJDK resolved this issue for me.
sudo update-alternatives --config java
sudo update-alternatives --config javac
Java 8 is deprecated, so downgrading to is must NOT be an acceptable solution.
The correct solution is to upgrade your SBT to 0.13.17 or higher.
This issue appears to be with Java 9 and gets solved with Java 8.
If you have Java 8 and Java 9, and you would like to keep both, you can set your environment variable JAVA_HOME to the Java 8 path.
For example:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home
You will need to replace jdk1.8.0_161.jdk with whichever version of the JDK that you have.
You can find that version with:
ls /Library/Java/JavaVirtualMachines/
Once you set that environment variable, you can open a new terminal and run java -version to see if the version was set correctly.
EDIT:
It may be worth noting that I have a Mac even though this is a linux question.
Installing java version "1.8.0_144" resolved the issue for me. I had installed jdk 1.9 previously.
If you're using an older version of Scala that is not compatible with JDK greater than 8, update to a newer version that's compatible with JDK 11. JDK 11 is current the LTS.
Versions by minor version:
For 2.11.x this means 2.11.12 or later
For 2.12.x this means 2.12.10 or later
For 2.13.x this means 2.13.1 or later
As other answers mentioned, make sure you're also using a build tool that's compatible with JDK >8 (e.g. sbt greater than 0.13.17).
Installing Java 8 resolved this issue.
I downloaded java 8 and i extracted that to the following location : /usr/lib/jvm/jdk1.8.0_172
After doing this update the JAVA_HOME path to the same : /usr/lib/jvm/jdk1.8.0_172
This will resolve the issue.
Worked with this java version "1.8.0_144"
Uninstall java and apache-spark
install java, install apache-spark
I ran into the same issue. Uninstall Java 9
sudo rm -fr /Library/Java/JavaVirtualMachines/jdk-9.jdk/
sudo rm -fr /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin
sudo rm -fr /Library/PreferencePanes/JavaControlPanel.prefPane
Ref: https://gist.github.com/schnell18/bcb9833f725be22f6acd01f94b486392
Install Java 8.
Everything seems to good now.
Btw: this was for mac. Might be useful for you.
i had a zip of my jdk at the same location. Removing the zip did the trick for me.

How to install an older version of scala

I currently had installed scala 2.11.7 but in order to fix this issue, I have to install a older version of scala(2.10.5). When I try to install with brew, I keep getting the following error
Error: No available formula for 2.10.5
I tried downloading the binaries for scala download page but when I load the SDK and all the libraries in IntelliJ, I just get this error
Error:scalac: No 'scala-library*.jar' in Scala compiler classpath in Scala SDK scala-sdk-2.10.5
So what is the right way to install an older version of scala and setting the same in intellij
The following should work for Homebrew 0.9.5:
Install Tapped version of Scala 2.10
brew install homebrew/versions/scala210
Unlink version of Scala 2.11 (if installed)
brew unlink scala
Link Tapped version of Scala 2.10
brew link scala210 --force
Check scala version
scala -version
brew search scala
then you will find all the available versions like below
scala scala#2.10 scala#2.11 scalaenv scalariform scalastyle
homebrew/science/scalapack Caskroom/cask/scala-ide
Then choose whichever version you want to install. say if you want to install scala 2.11 then you can do that by runnig the command like below
brew install scala#2.11
If you manage your project with sbt, the scala built tool, either directly or via IntelliJ, the version of scala for that code is defined in the build file (build.sbt or Build.scala):
scalaVersion := "2.10.5"
Choosing this path, there isn't even a need to install scala on your Mac at all. Each project will have its own version -- pulled from your local ivy repository, so it doesn't have to download it each time.
And rather than typing scala to get to the REPL, you type sbt console to get to the REPL with the project's scala version and dependencies loaded.
Homebrew installs scala 2.10.5 with the formula scala210:
$ brew install scala210
==> Installing scala210 from homebrew/homebrew-versions
==> Downloading https://homebrew.bintray.com/bottles/scala210-2.10.5.yosemite.bottle.tar.gz
==> Downloading http://www.scala-lang.org/files/archive/scala-2.10.5.tgz
######################################################################## 100,0%
However, if you are using a build tool like maven or sbt, you should be able to set another scala version in your build config. You should then be able to import your project into IntelliJ and IntelliJ should automatically use the dependencies defined in your build config.
Using brew to install specific version:
$ brew search scala
==> Formulae
scala scala#2.10 scala#2.11 scalaenv
scalapack scalariform scalastyle
==> Casks
scala-ide
Make sure you default version in profile
$echo 'export PATH="/usr/local/opt/scala#2.11/bin:$PATH"' >> ~/.bash_profile

How to install older version of sbt?

I am facing some "UNRESOLVED DEPENDENCIES" problem for my custom dependencies with latest version of sbt(0.13.8)
I want to install older version of sbt, Do we have any command by which I can install older version of sbt ?
You can define the sbt version by writing the one you need in /project/build.properties
It would be something like:
sbt.version=x.xx.x
Use the following command for a Debian-based Linux:
sudo apt-get install sbt=0.13.13