The magic %classpath
adds jars to the kernel. And the magics %import
and %unimport
control which classes are visible by default in your code (the regular import
command is local to the cell where it runs.
These magics work in all the BeakerX JVM kernels.
This first cell shows that you get an error if you try to import a class not built-in to BeakerX:
import com.example.Demo
Then load a jar into the kernel:
%classpath add jar ../resources/jar/demo.jar
After that, it imports and runs:
import com.example.Demo
Demo demo = new Demo();
println demo.getObjectTest()
You can add multiple jars. Wildcards also work, like they normally do in the classpath (in this case the code could say ../resources/jar/*
).
%classpath add jar ../resources/jar/demo.jar
%classpath add jar ../resources/jar/BeakerXClasspathTest.jar
println com.example.Demo.staticTest();
import com.example.Demo
Demo demo = new Demo();
println demo.getObjectTest()
import com.beaker.BeakerXClasspathTest
BeakerXClasspathTest t = new BeakerXClasspathTest();
println com.beaker.BeakerXClasspathTest.staticTest;
println t.getObjectTest();
OutputCell.HIDDEN
The source code for these jars is in the repository. See Demo.java, and BeakerXClasspathTest.java
With no arguments the classpath magic prints-out all loaded jars:
%classpath
@Grab(group='com.google.code.gson', module='gson', version='2.2.4')
import com.google.gson.GsonBuilder
new GsonBuilder().create().toJson("Hello", System.out)
The %classpath
magic also supports loading from maven central:
%classpath add mvn com.google.code.gson gson 2.2.4
new com.google.gson.GsonBuilder().create().toJson("Hello", System.out)
You can either use gradle like syntax to load dependency
%classpath add mvn com.sun.jersey:jersey-core:1.19.4
import com.sun.jersey.api.uri.UriBuilderImpl
return new UriBuilderImpl()
.path("http://beakerx.com/")
.path("documentation")
.build();
The %classpath config resolver
gives you able to define custom repository
%classpath config resolver repository.spring.snapshot http://repo.spring.io/snapshot
%classpath add mvn org.springframework spring-context 5.0.3.BUILD-SNAPSHOT
You can use %%classpath
cell magic to load multiple dependencies at once. Cell magic will create POM file that will contain all dependencies listed in cell.
%%classpath add mvn
org.slf4j slf4j-api 1.7.25
org.slf4j slf4j-nop 1.7.25
The above magics work on literal strings. If you need to compute the location of a jar, use the dynamic classpath magic:
location = "../resources/jar/"
%classpath add dynamic location + "demo.jar"
%classpath add dynamic [location + "demo.jar", location + "BeakerXClasspathTest.jar"]
Normally import
in Groovy only works in the cell where you use it. To make a class import automatically into all cells, use %import
magic.
%import com.twosigma.beakerx.widget.IntSlider
w = new IntSlider()
w.value = 60
w
w2 = new IntSlider()
%unimport com.twosigma.beakerx.widget.IntSlider
w3 = new IntSlider()
%import static java.lang.Math.PI
PI
More details of the implementation:
%classpath add jar ../resources/jar/demo.jar
%classpath
5+5
%classpath
%classpath add jar ../resources/jar/demo.jar
%classpath add jar ../resources/jar/demo.jar
%classpath
%import static com.example.Demo.staticTest
staticTest()
%import static com.example.Demo.STATIC_TEST_123
STATIC_TEST_123
%import static com.example.Demo.*
STATIC_TEST_123
staticTest()