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

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.

Related

Can't seem to uninstall scala with Homebrew

I'm trying to uninstall scala so I can redownload an older version for compatibility reasons. I thought I had done it but whenever I run scala, it still runs the latest version, the one I thought I had uninstalled. This is the code I've been running in my terminal, I'm on a macbook also, anymore info needed lmk, any help is much appreciated!
➜ ~ scala
Welcome to Scala 3.2.0 (18.0.2.1, Java Java HotSpot(TM) 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
scala> :q
➜ ~ brew list scala
Error: No such keg: /usr/local/Cellar/scala
➜ ~ brew uninstall scala
Error: No such keg: /usr/local/Cellar/scala

SBT fails with `String.class is broken`

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

:javap -p <classname> returns IlegalArgumentException

Getting below error in Scala REPL
scala> :javap -p Add
java.lang.IllegalArgumentException: requirement failed
at scala.Predef$.require(Predef.scala:264)
at scala.tools.nsc.interpreter.JavapClass$JavapTool$.apply(JavapClass.scala:337)
at scala.tools.nsc.interpreter.JavapClass.tool$lzycompute(JavapClass.scala:37)
at scala.tools.nsc.interpreter.JavapClass.tool(JavapClass.scala:37)
at scala.tools.nsc.interpreter.JavapClass.apply(JavapClass.scala:49)
at scala.tools.nsc.interpreter.ILoop.javapCommand(ILoop.scala:332)
at scala.tools.nsc.interpreter.ILoop.$anonfun$standardCommands$6(ILoop.scala:187)
at scala.tools.nsc.interpreter.LoopCommands$LineCmd.apply(LoopCommands.scala:132)
at scala.tools.nsc.interpreter.LoopCommands.colonCommand(LoopCommands.scala:103)
at scala.tools.nsc.interpreter.LoopCommands.colonCommand$(LoopCommands.scala:101)
at scala.tools.nsc.interpreter.ILoop.colonCommand(ILoop.scala:44)
at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:696)
at scala.tools.nsc.interpreter.ILoop.processLine(ILoop.scala:416)
at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:437)
at scala.tools.nsc.interpreter.ILoop.$anonfun$process$1(ILoop.scala:1012)
at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:909)
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:75)
at scala.tools.nsc.MainGenericRunner.run$1(MainGenericRunner.scala:88)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:99)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:104)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
That entry seems to have slain the compiler. Shall I replay
your session? I can re-run each line except the last one.
[y/n]
Abandoning crashed session.
Please share if anyone encountered this issue & able to resolve. Below is the versions information. Thanks in advance!
C:\Users\email>scala -version
Scala code runner version 2.12.2 -- Copyright 2002-2017, LAMP/EPFL and Lightbend, Inc.
C:\Users\email>java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
As the answer said Why does installing Scala require additionally installing JDK?
Maybe you just installed Jre not JDK.
To solve the problem, install the corresponding version JDK.
:javap command within Scala REPL invoke the javap tool installed by JAVA SDK via java.util.spi.ToolProvider[ver2.13.1]. Jre don't include those JDK tools such as javap, jdb, jar.

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

Getting rid of old scala version and install 2.11.7

Noob question incoming.
I just uninstalled scala 2.9.2 from my machine in doing:
sudo apt-get remove scala
While the command:
scala -version
outputs:
The program 'scala' is currently not installed. You can install it by typing: sudo apt-get install scala
My scala/sbt program still functions...
Why? I do not know.
I would like to install the latest version of scala which is the 2.11.7.
My questions are:
- How do I fully uninstall scala?
- How do I install scala 2.11.7? (In which directory uncompress the .tgz? What bindings?)
SBT is not scala
the command sbt usually is a wrapper script that launches sbt-launch.jar in [home]/.sbt/launchers. Where that wrapper script is installed to heavily depends on how you installed it.
SBT downloads its own scala version into a projects target path. It is totally local, so imo there is not reason to worry about installation relics.
Thus, if you want to use scala 2.11.7, just set scalaVersion := 2.11.7 in your build.sbt