Skip to content

Instantly share code, notes, and snippets.

@scottdraves
Created February 2, 2018 04:17
Show Gist options
  • Select an option

  • Save scottdraves/7de0613c9d9102b51a9ab34c98842cdb to your computer and use it in GitHub Desktop.

Select an option

Save scottdraves/7de0613c9d9102b51a9ab34c98842cdb to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"beakerx_kernel_parameters": {},
"kernelspec": {
"display_name": "Groovy",
"language": "groovy",
"name": "groovy"
},
"language_info": {
"codemirror_mode": "groovy",
"file_extension": ".groovy",
"mimetype": "",
"name": "Groovy",
"nbconverter_exporter": "",
"version": "2.4.3"
}
},
"nbformat_minor": 2,
"nbformat": 4,
"cells": [
{
"cell_type": "markdown",
"source": "# Manipulating the Java Classpath and Imports\n\nThe magic `%classpath` allows you to add jars to your kernel. And the magics `%import` and `%unimport` control which classes are visible by default in your code. These magics work in all the BeakerX JVM kernels.\n\nThis first cell shows that you get an error if you try to import a class not built-in to BeakerX:",
"metadata": {}
},
{
"cell_type": "code",
"source": "import com.example.Demo",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": "Then load a jar ([source code](https://github.com/twosigma/beakerx/blob/master/kernel/demoProjects/demo/src/main/java/com/example/Demo.java)) into the kernel with this magic:",
"metadata": {}
},
{
"cell_type": "code",
"source": "%classpath add jar ../resources/jar/demo.jar",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": "Then that class imports and runs:",
"metadata": {}
},
{
"cell_type": "code",
"source": "import com.example.Demo\nDemo demo = new Demo();\nprintln demo.getObjectTest()",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": "You can add multiple jars ([source code](https://github.com/twosigma/beakerx/blob/master/kernel/demoProjects/BeakerXClasspathTest/src/main/java/com/beaker/BeakerXClasspathTest.java) for BeakerXClasspathTest).\n\nWildcards also work.",
"metadata": {}
},
{
"cell_type": "code",
"source": "%classpath add jar ../resources/jar/demo.jar\n%classpath add jar ../resources/jar/BeakerXClasspathTest.jar\n\nprintln com.example.Demo.staticTest();\n\nimport com.example.Demo\n\nDemo demo = new Demo();\nprintln demo.getObjectTest()\n\nimport com.beaker.BeakerXClasspathTest\nBeakerXClasspathTest t = new BeakerXClasspathTest();\nprintln com.beaker.BeakerXClasspathTest.staticTest;\nprintln t.getObjectTest();\nOutputCell.HIDDEN",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": "With no arguments it prints-out all loaded jars:",
"metadata": {}
},
{
"cell_type": "code",
"source": "%classpath",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": "## Loading single Dependency with Grapes and Maven\n\nGroovy has a dependency manager called [Grape](http://docs.groovy-lang.org/latest/html/documentation/grape.html) built-in and you can access it as follows:",
"metadata": {}
},
{
"cell_type": "code",
"source": "@Grab(group='com.google.code.gson', module='gson', version='2.2.4')\nimport com.google.gson.GsonBuilder\nnew GsonBuilder().create().toJson(\"Hello\", System.out)",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": "The `%classpath` magic also supports loading from [maven central](https://search.maven.org):",
"metadata": {}
},
{
"cell_type": "code",
"source": "%classpath add mvn com.google.code.gson gson 2.2.4\nnew com.google.gson.GsonBuilder().create().toJson(\"Hello\", System.out)",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": "You can either use gradle like syntax to load dependency",
"metadata": {}
},
{
"cell_type": "code",
"source": "%classpath add mvn com.sun.jersey:jersey-core:1.19.4\nimport com.sun.jersey.api.uri.UriBuilderImpl\nreturn new UriBuilderImpl()\n .path(\"http://beakerx.com/\")\n .path(\"documentation\")\n .build();",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": "The `%classpath config resolver` gives you able to define custom repository",
"metadata": {}
},
{
"cell_type": "code",
"source": "%classpath config resolver repository.spring.snapshot http://repo.spring.io/snapshot",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": "%classpath add mvn org.springframework spring-context 5.0.3.BUILD-SNAPSHOT",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": "## Loading multiple Dependencies with Maven\n\nYou 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.",
"metadata": {}
},
{
"cell_type": "code",
"source": "%%classpath add mvn\norg.slf4j slf4j-api 1.7.25\norg.slf4j slf4j-nop 1.7.25",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": "## Loading Jars from a Dynamic Location\n\nThe above magics work on literal strings. If you need to compute the location of a jar, use the dynamic classpath magic:",
"metadata": {}
},
{
"cell_type": "code",
"source": "location = \"../resources/jar/\"",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": "%classpath add dynamic location + \"demo.jar\"",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": "%classpath add dynamic [location + \"demo.jar\", location + \"BeakerXClasspathTest.jar\"]",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": "## Import and Unimport\n\nNormally `import` in Groovy only works in the cell where you use it. To make a class import automatically into all cells, use `%import` magic.",
"metadata": {}
},
{
"cell_type": "code",
"source": "%import com.twosigma.beakerx.widgets.integers.IntSlider\nw = new IntSlider()\nw.value = 60\nw",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": "w2 = new IntSlider()",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": "%unimport com.twosigma.beakerx.widgets.integers.IntSlider",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": "w3 = new IntSlider()",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": "%import static java.lang.Math.PI",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": "PI",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": "More details of the implementation:",
"metadata": {}
},
{
"cell_type": "code",
"source": "%classpath add jar ../resources/jar/demo.jar\n%classpath\n5+5",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": "%classpath\n%classpath add jar ../resources/jar/demo.jar",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": "%classpath add jar ../resources/jar/demo.jar\n%classpath",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": "%import static com.example.Demo.staticTest",
"metadata": {},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment