1

I was following the step on https://github.com/SpencerPark/IJava to install Jave on my Jupyter Notebook. Everything was fine, I was able to execute jupyter kernelspec list and find my Java Kernal: java /Users/CodingStark/opt/anaconda3/share/jupyter/kernels/java. But when I try to open a Java script on my Jupyter Notebook, it kept saying Kernal Failed. So I tried to execute this command jupyter console --kernel=javaon my Terminal, it gave me an error: OSError: [Errno 8] Exec format error: '/Users/CodingStark/opt/anaconda3/share/jupyter/kernels/java/ijava-1.3.0.jar'. I am wondering how can I fix this and be able to use Java on my Jupyter Notebook.

Here is the kernal.json file inside the Java folder. enter image description here

CodingStark
  • 199
  • 3
  • 17
  • Could you add the contents of the `kernel.json` in the `.../kernels/java` folder? Particularly curious about the `argv` line and if it was modified. – SpencerPark Jul 18 '20 at 06:32
  • @SpencerPark Just add a screenshot of the kernel.json file – CodingStark Jul 18 '20 at 09:07
  • Thanks @CodingStark, the answer below should fix the problem but I would also like to know if the installer generated that or if you modified it after the fact? – SpencerPark Jul 18 '20 at 09:14
  • @SpencerPark Thank you so much! I actually modified it on my own before due to other Error. – CodingStark Jul 18 '20 at 17:51

1 Answers1

1

The argv array is the command that jupyter runs to start the kernel. It should be something like:

  "argv": [
    "java",
    "-jar",
    "/Users/CodingStark/opt/anaconda3/share/jupyter/kernels/java/ijava-1.3.0.jar",
    "{connection_file}"
  ]

Unless you want to use a different absolute path to the jdk you want the kernel to use, but usually just "java" is fine (will use whatever is first in the $PATH).

If you get an error saying that the kernel class file version is 53.0 (means compiled with java 9) but the current runtime only recognizes a class file version lower than than (like 52.0 for java 8), then this means the java in your $PATH is too old.

You can keep your existing installation as is but will need to install a newer jdk as well. To keep java 8 in your path but use something newer in Jupyter, change the kernel.json to point to that version of java specifically (for example, on osx):

  "argv": [
    "/Library/Java/JavaVirtualMachines/openjdk-14.0.1.jdk/Contents/Home/bin/java",
    "-jar",
    "/Users/CodingStark/opt/anaconda3/share/jupyter/kernels/java/ijava-1.3.0.jar",
    "{connection_file}"
  ]
SpencerPark
  • 3,298
  • 1
  • 15
  • 27
  • I tried to modify the json file and add the `java` and `-jar` line back. But when I tried to run the command `jupyter console --kernel=java` again. It gave me this error: `Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.UnsupportedClassVersionError: io/github/spencerpark/ijava/IJava has been compiled by a more recent version of the Java Runtime (class file version 53.0), this version of the Java Runtime only recognizes class file versions up to 52.0` – CodingStark Jul 18 '20 at 17:53
  • 1
    @CodingStark the edit should help with your original issue – SpencerPark Jul 18 '20 at 23:54
  • Thank you so much! So is that mean I can just use this line `"/Library/Java/JavaVirtualMachines/openjdk-14.0.1.jdk/Contents/Home/bin/java"` to replace the original `"java` line? – CodingStark Jul 19 '20 at 02:18
  • My pleasure, yes provided that is where you have the more recent version of jdk installed :) – SpencerPark Jul 19 '20 at 03:40
  • 1
    Thank you so much! The Java works fine now on my Jupyter Notebook! – CodingStark Jul 19 '20 at 06:13
  • @SpencerPark Hi, I tried to change ```"java"``` to ```"/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/bin/java"``` but still the same error appears. I checked the java version by ```java -version``` and it gives openjdk version "11.0.9.1". Do you have any idea why it is not working? Thank you! – Zhang Yongheng Oct 13 '21 at 05:58