58

When I type scala on the terminal to start the repl, it throws this error

scala> [init] error: error while loading AnnotatedElement, 
class file '/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar
(java/lang/reflect/AnnotatedElement.class)' 
is broken (bad constant pool tag 15 at byte 2713)

When I hit enter and type println("hello, world"), it again throws this

error: error while loading CharSequence, 
class file '/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar
(java/lang/CharSequence.class)' is broken
(bad constant pool tag 15 at byte 1501)

I am using Ubuntu 14.04 and java -version gives

java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)
ajay
  • 9,402
  • 8
  • 44
  • 71
  • What version of Scala do you have? – izstas Jul 01 '14 at 19:29
  • scala -version gives `Scala code runner version 2.9.2 -- Copyright 2002-2011, LAMP/EPFL` – ajay Jul 01 '14 at 19:31
  • 1
    possible duplicate of [Compilation failed: error while loading AnnotatedElement, ConcurrentMap, CharSequence from Java 8 under Scala 2.10?](http://stackoverflow.com/questions/24197836/compilation-failed-error-while-loading-annotatedelement-concurrentmap-charseq) – Dielson Sales Sep 10 '14 at 06:19
  • Possible duplicate of [Scala REPL startup error “class file is broken”](http://stackoverflow.com/questions/18842985/scala-repl-startup-error-class-file-is-broken) – Yehor Nemov Sep 25 '14 at 08:28
  • In addition to updating the Scala version, I also had to update the SBT version in the project's properties to fix this. – Peter Becich Mar 21 '15 at 19:43

4 Answers4

63

Either update to a newer scala version (2.10.3+) or downgrade java to java 6/7. As you have seen in the output, 2.9.2 was here long before java 8 was introduced (Copyright 2002-2011, LAMP/EPFL), so they don't work well together.

This duplicate question contains exact instructions on ubuntu's java downgrade.

Community
  • 1
  • 1
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
  • It feels to be logically impossible to ancestor to be duplicate of its successor (this one). That's why your flag that [far earlier question](http://stackoverflow.com/questions/18842985/scala-repl-startup-error-class-file-is-broken) is duplicate (see "the question is asked before and (but not OR) already has an answer") of this one is a bit inaccurate. But you're right in a fact that current post has explicit answer whilst the earlier has implicit one and _de jure_ is unanswered. – Yehor Nemov Jul 03 '14 at 11:00
  • 1
    @ghoshak I generally prefer older questions, but, yeah, closed that one because it had no accepted answers and existing question proposed only one way to solve problem. This post had a better chances in this perspective. – om-nom-nom Jul 03 '14 at 11:03
  • Yeah, your side are proper too but where have you been six months ago. – Yehor Nemov Jul 03 '14 at 11:22
  • I don't understand why 2.10.3 is a newer version than 2.9.2 ? – code4j Dec 20 '15 at 17:11
  • @code4j it's like apartments #3 on 10th floor are higher than apartments #2 on 9th floor. – om-nom-nom Dec 21 '15 at 20:56
13

remove the scala 2.9.2 using terminal

 sudo apt-get remove scala 

download the latest scala from scala website

Installation instruction are giving on this link

Hafiz Shehbaz Ali
  • 2,566
  • 25
  • 21
1

You might have to run it with JDK 7 or 6

Rakesh Sharma
  • 301
  • 1
  • 3
  • 11
  • by setting the environment variable for example: `export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/` – panchicore Nov 10 '17 at 11:20
0

Downgrading JDK version to 7 solved this issue for me. Here is the solution: https://askubuntu.com/questions/761127/how-do-i-install-openjdk-7-on-ubuntu-16-04-or-higher

Sabi
  • 21
  • 2
  • A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](https://meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted](https://stackoverflow.com/help/deleted-answers). – Sᴀᴍ Onᴇᴌᴀ Aug 28 '17 at 16:43