2

I am trying to use java in jupyter notebook using SpencerPark IJava.

After the successful installations I opened a new java notebook but it is not connecting with the jupyter notebook.

I tried running this command given on the official docs of SpencerPark IJava in anaconda prompt

jupyter console --kernel=java

and it is throwing this exception:

Exception in thread "main" java.lang.UnsupportedClassVersionError: io/github/spencerpark/ijava/IJava : Unsupported major.minor version 53.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

the same error is showing in jupyter notebook running server.

Anyone knows how to resolve this?

  • No, my kernel is not starting here, my java codes are running properly in other idles. So this solution is not helping. – Nadeem Ahmed Mar 13 '21 at 14:46
  • You seem to misunderstand the error. The code was compiled with a more recent JDK version than the JRE trying to run it, hence the `UnsupportedClassVersionError`. – Turing85 Mar 13 '21 at 15:48

1 Answers1

1

IJava is compiled for java 9 (as this is the version that introduces JShell). This means the kernel needs to run on a JVM that is version 9 or higher. That is what the Unsupported major.minor version 53.0 is suggesting.

Typically users that run into this problem have installed a correct version but they have an older JVM that is being used instead of their newly installed one. There are 2 options to fix this:

  1. Tell Jupyter exactly what version of java to start the kernel with by changing the kernel.json:

    • Execute jupyter kernelspec list to find the install directory of the kernel.

      Available kernels:
        java           C:\Users\Spencer\AppData\Roaming\jupyter\kernels\java
        ...
      
    • In that directory find kernel.json and change argv. The kernel.json is a configuration file that also tells jupyter how to start the kernel. The argv is simply a command to execute. NOTE: use / or when using \ you must escape them in JSON by writing \\ instead.

      {
          "argv": [
              // "java",
              "C:/absolute/path/to/jdk-9/bin/java"
              "-jar",
              "C:/Users/Spencer/AppData/Roaming/jupyter/kernels/java/ijava-1.3.0.jar",
              "{connection_file}"
          ],
          ...
      }
      

      I will also mention here, env is a dictionary of environment variables that Jupyter will set before calling the argv. You can use this to set JAVA_HOME or other variables that you always want set when running the kernel. On Mac this seems to be what users are more familiar with.

      {
          ...
          "env": {
              "JAVA_HOME": "/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home"
          }
      }
      
    • Optionally, you may want to also change the display_name to include the version. This is handy if you have a few versions setup so when you use the dropdown to pick a new kernel it will say Java 9 (for example).

  2. Add java 9 temporarily to your path before starting the notebook.

    • Like mentioned above the kernel just executes the java command to start up the kernel. This means that we can stick the more recent java version at the front of your path.
    • set PATH=C:\absolute\path\to\jdk-9\bin\;%PATH% will do this.
      • As a side note I have some variables JAVA7_HOME, JAVA8_HOME, JAVA9_HOME to simplify this switch. Then I use set PATH=%JAVA9_HOME%\bin\;%PATH% to use a certain version.
    • Then when using where java it may give multiple paths but the one at the top of the list should be java 9.
    • Finally launch the notebook with this new path and you should be good to go. Putting everything together
      set PATH=%JAVA9_HOME%\bin\;%PATH% && jupyter notebook
      
      or
      export PATH=$JAVA9_HOME/bin/:$PATH && jupyter notebook
      
SpencerPark
  • 3,298
  • 1
  • 15
  • 27
  • Hey @SpencerPark , could this be the cause for this problem also? --> https://stackoverflow.com/questions/74674688/google-colab-notebook-using-ijava-stuck-at-connecting-after-installation-ref/74709951 – thomers Dec 07 '22 at 12:03