scijava / scijava-jupyter-kernel

[RETIRED] Try IJava or BeakerX
Apache License 2.0
178 stars 42 forks source link

Can I actually write a notebook in Java? #71

Closed gbivins closed 7 years ago

gbivins commented 7 years ago

This looks great! Being able to write a Notebook with multiple langs will really help us collaborate with the different functions on our team. If I understand correctly, the base kernel is written in Java so I tried and the kernel kept dying. This is what his notebook code looks like:

#! java
System.out.println("DO IT");
System.out.println("YUNOCANZPRINT");

I also tried defining an actual class but that didn't work either. There aren't any examples using Java so the question is does this kernel support writing code in Java?

ctrueden commented 7 years ago

Why not use BeanShell, or Groovy? They are very close to Java, but without the problems described below.

There is a scripting-java language but there are two issues:

  1. It needs a Java compiler available, and at the moment does not always discover the correct one.
  2. You need to write a valid class declaration.

The kernel crashing sounds like a bug, though. Can you get some debugging output surrounding the crash?

gbivins commented 7 years ago

Ah, the separate scirpt language seems to be the issue. Looking in the console I see
jupyter-java

So I guess I need to install "scripting-java" to try it out. I'm too new to this so I'll just ask, how do I "install scripting-java" so that scijava-jupyter can find it at runtime?

I will also look into Groovy

hadim commented 7 years ago

Java scripting language is bugged. First, you have to use Java and not java then it will be found (all the scripting languages are shipped by default when you install the kernel).

But this is not sufficient to make it work.

#! Java

class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

produces the following error:

[INFO] Script Language for 'Java' found.d. to use.
INFO] Script Language for 'Java' found.d. to use.Exception in thread "Thread-2" java.lang.UnsupportedOperationException
ava.lang.UnsupportedOperationException  at org.scijava.plugins.scripting.java.JavaEngineBindings.put(JavaEngineBindings.java:92)
at org.scijava.plugins.scripting.java.JavaEngineBindings.put(JavaEngineBindings.java:92)    at org.scijava.jupyter.kernel.evaluator.ScijavaEvaluator.lambda$initBindings$0(ScijavaEvaluator.java:224)
at org.scijava.jupyter.kernel.evaluator.ScijavaEvaluator.lambda$initBindings$0(ScijavaEvaluator.java:224)   at java.util.HashMap$KeySet.forEach(HashMap.java:932)tor.lambda$initBindings$0(ScijavaEvaluator.java:224)
at java.util.HashMap$KeySet.forEach(HashMap.java:932)tor.lambda$initBindings$0(ScijavaEvaluator.java:224)   at org.scijava.jupyter.kernel.evaluator.ScijavaEvaluator.initBindings(ScijavaEvaluator.java:223)java:224)
at org.scijava.jupyter.kernel.evaluator.ScijavaEvaluator.initBindings(ScijavaEvaluator.java:223)java:224)   at org.scijava.jupyter.kernel.evaluator.ScijavaEvaluator.addLanguage(ScijavaEvaluator.java:189))java:224)

Line 224 of ScijavaEvaluator.java is related to bindings synchronization:

    private void initBindings(Bindings bindings, ScriptEngine scriptEngine, ScriptLanguage scriptLanguage) {

    Bindings currentBindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
    bindings.keySet().forEach((String key) -> {
        currentBindings.put(key, scriptLanguage.decode(bindings.get(key)));
    });

    }

It needs more investigations.

ctrueden commented 7 years ago

I definitely recommend people use Groovy instead for now, since these issues with scripting-java are unlikely to be fixed in the near future.

hadim commented 7 years ago

Good point, I personally don't see any advantage using Java instead of Groovy or Scala in a notebook. That being said that would be nice if a Java kernel could actually execute Java code :-)

gbivins commented 7 years ago

Could there be a problem if currentBindings == bindings ,
ie iterating thekeySet while also trying to modify it with put?

ghost commented 7 years ago

I personally don't see any advantage using Java instead of Groovy or Scala in a notebook.

coming from the python world, my use case is "learning java"

ctrueden commented 7 years ago

coming from the python world, my use case is "learning java"

You can write 99% syntactically correct Java in Groovy. In other words: you can learn Java by selecting the Groovy script language and then writing Java code. The only major exception I know of is the for-each loops, which in Groovy use the in keyword instead of Java's colon :.

aviniciux commented 5 years ago

I'm successfully install that java kernel without using anaconda: https://github.com/SpencerPark/IJava

I'm using Linux Fedora 28 64-bit, so this following examples work for me (not sure if you'll need to adapt in any way).

First, verify your java installation. I used sdk 11.0.1 downloaded from here:

Next, install the kernel: after unzip, goes to the folder and runs the follow command:

chmod u+x gradlew && ./gradlew installKernel

Alternatively, you can try this one:

https://github.com/SpencerPark/IJava/releases/download/v1.2.0/ijava-1.2.0.zip

and install just with the command-line:

python3 install.py

Read the readme file carefully on: https://github.com/SpencerPark/IJava

Holpe it help you.