How can I use Java in Google Colab

Sören Etler picture Sören Etler · Jul 11, 2018 · Viewed 11.8k times · Source

I want to use Stanford CoreNLP in my Google Colab Notebook. For that I need Java. Is there a way to install Java on those machines?

What I currently have is:

!pip install StanfordCoreNLP
from stanfordcorenlp import StanfordCoreNLP
nlp = StanfordCoreNLP('stanford-corenlp', lang='de', memory='4g')
...
nlp.close()

and I get the error:

FileNotFoundError: [Errno 2] No such file or directory: 'java': 'java'

Answer

Sanjeev v picture Sanjeev v · Aug 9, 2018

try this

import os       #importing os to set environment variable
def install_java():
  !apt-get install -y openjdk-8-jdk-headless -qq > /dev/null      #install openjdk
  os.environ["JAVA_HOME"] = "/usr/lib/jvm/java-8-openjdk-amd64"     #set environment variable
  !java -version       #check java version
install_java()