How to change which version of Java to use in Jupyter Notebook

Viewed 2226

I am launching Jupyter from the Anaconda prompt. However, I seem to be running into some errors because when I do ! echo %JAVA_HOME% in the notebook it points to somewhere that has Java 11, when I need Java 8.

I do echo %JAVA_HOME% in Anaconda prompt and get that same path for Java 11. When I do set JAVA_HOME='___' (path to Java 8) and then print the value of the variable again, it gives me the right path that I want to the Java 8 location. But then I go back to the notebook and run the same command and it still shows as the Java 11 path.

When I print JAVA_HOME in Command prompt, it gives me the Java 8 path that I want, but not when I use the Anaconda prompt. How can I change it in Anaconda/Jupyter notebook?

1 Answers

The following code block can be used to modify the Java version within the Jupyter notebook.

here I'm changing the Java version from 11 to 8.

  1. Identify the installed Java versions by executing: !/usr/libexec/java_home -V in Jupyter (for Terminal execute it without the !)
  2. Replace the java_path value to your desired Java version in the code block below
java_path = "/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home"
java_home = os.environ.get('JAVA_HOME', None)

print("Existing Java Home: ",java_home)

if (not java_home) or (java_path not in java_home):
    os.environ['JAVA_HOME'] = java_path

print("Updated Java Home: ",os.environ.get('JAVA_HOME', None))
Existing Java Home:  /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
Updated Java Home:  /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
Related