52

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?

Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384

9 Answers9

40

Using the Java 8 version of OpenJDK resolved this issue for me.

sudo update-alternatives --config java
sudo update-alternatives --config javac
WOPR
  • 5,313
  • 6
  • 47
  • 63
  • 1
    installing specific java version on osx with brew: https://www.lonecpluspluscoder.com/2017/10/08/installing-other-versions-of-the-java-jdk-via-homebrew/ – polo Oct 11 '17 at 18:02
  • Very Helpful answer! – Praveen Kumar K S Jan 30 '18 at 10:20
  • This is a linux question, but if you are having this issue on a Mac, follow these instructions to install Java8 in your system (https://stackoverflow.com/questions/24342886/how-to-install-java-8-on-mac), and set your new JAVA_HOME accordingly (see @Dbz answer below). – Rick Mar 03 '18 at 19:42
  • I was using Java 10, and downgrading to version 8 of OpenJDK indeed worked! However, I also had to update the JAVA_HOME environment variable to the proper folder for it to work, in case anyone needs to do this as well. – anaotha Oct 04 '18 at 13:58
  • Using the Java 8 version worked for me on Ubuntu without any further change. Great answer – randombee Jan 10 '19 at 19:47
  • As mentioned, downgrade to legacy software is an bad option. This is not an correct answer. Only option is to update scala and sbt – judovana Aug 03 '20 at 13:29
15

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.

silverberry
  • 786
  • 5
  • 20
10

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.

Dbz
  • 2,721
  • 4
  • 35
  • 53
6

Installing java version "1.8.0_144" resolved the issue for me. I had installed jdk 1.9 previously.

Yash P Shah
  • 779
  • 11
  • 15
5

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

cstroe
  • 3,740
  • 3
  • 27
  • 16
2

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.

S.P.
  • 185
  • 19
1

Worked with this java version "1.8.0_144"

Uninstall java and apache-spark

install java, install apache-spark

Anthony Awuley
  • 3,455
  • 30
  • 20
-1

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.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
Murali Bala
  • 1,133
  • 2
  • 18
  • 28
-2

i had a zip of my jdk at the same location. Removing the zip did the trick for me.

cs95
  • 379,657
  • 97
  • 704
  • 746