Search code examples
javapysparkgoogle-colaboratory

How to change the Java version in Google Colab?


I'm trying to run pyspark on Google colab, which requires Java 8, but when I look at the java version in Colab, it gives:

openjdk version "11.0.4" 2019-07-16 OpenJDK Runtime Environment (build 11.0.4+11-post-Ubuntu-1ubuntu218.04.3) OpenJDK 64-Bit Server VM (build 11.0.4+11-post-Ubuntu-1ubuntu218.04.3, mixed mode)

I tried to change the Java version by using:

!apt-get install openjdk-8-jdk-headless -qq > /dev/null import os os.environ["JAVA_HOME"] = "/usr/lib/jvm/java-8-openjdk-amd64"

However, the java version still stays 11. This method worked back in July, when I used Colab then, but now also runs an error on that same notebook. I'm not sure if this is a new thing, but I can't seem to find anything to remedy this.

For reference, I was trying to use .count() on a spark dataframe, but kept getting

IllegalArgumentException: 'Unsupported class file major version 55'


Solution

  • Sounds like you also need to update-alternatives:

    !apt-get install openjdk-8-jdk-headless -qq > /dev/null
    import os
    os.environ["JAVA_HOME"] = "/usr/lib/jvm/java-8-openjdk-amd64"
    !update-alternatives --set java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
    !java -version
    

    prints for me:

    openjdk version "1.8.0_222"
    OpenJDK Runtime Environment (build 1.8.0_222-8u222-b10-1ubuntu1~18.04.1-b10)
    OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)