{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Manipulating the Java Classpath and Imports\n", "\n", "The 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", "\n", "This first cell shows that you get an error if you try to import a class not built-in to BeakerX:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[link to Demo.java](https://github.com/twosigma/beakerx/blob/master/kernel/demoProjects/demo/src/main/java/com/example/Demo.java)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import com.example.Demo" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then load a jar into the kernel with this magic" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%classpath add jar ../resources/jar/demo.jar" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then it imports and runs" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import com.example.Demo" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import com.example.Demo\n", "Demo demo = new Demo();\n", "println demo.getObjectTest()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can add multiple jars. Wildcards also work." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[link to BeakerXClasspathTest.java](https://github.com/twosigma/beakerx/blob/master/kernel/demoProjects/BeakerXClasspathTest/src/main/java/com/beaker/BeakerXClasspathTest.java)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%classpath add jar ../resources/jar/demo.jar\n", "%classpath add jar ../resources/jar/BeakerXClasspathTest.jar\n", "\n", "println com.example.Demo.staticTest();\n", "\n", "import com.twosigma.beakerx.jvm.object.OutputCell\n", "import com.example.Demo\n", "\n", "Demo demo = new Demo();\n", "println demo.getObjectTest()\n", "\n", "import com.beaker.BeakerXClasspathTest\n", "BeakerXClasspathTest t = new BeakerXClasspathTest();\n", "println com.beaker.BeakerXClasspathTest.staticTest;\n", "println t.getObjectTest();\n", "OutputCell.HIDDEN" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With no arguments you can printout all loaded jars." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%classpath" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Loading Dependencies with Grapes and Maven\n", "\n", "Groovy 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:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "@Grab(group='com.google.code.gson', module='gson', version='2.2.4')\n", "import com.google.gson.GsonBuilder\n", "new GsonBuilder().create().toJson(\"Hello\", System.out)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `%classpath` magic also supports loading from [maven central](https://search.maven.org):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%classpath add mvn com.google.code.gson gson 2.2.4\n", "new com.google.gson.GsonBuilder().create().toJson(\"Hello\", System.out)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can either use gradle like syntax to load dependency" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%classpath add mvn com.sun.jersey:jersey-core:1.19.4\n", "import com.sun.jersey.api.uri.UriBuilderImpl\n", "return new UriBuilderImpl()\n", " .path(\"http://beakerx.com/\")\n", " .path(\"documentation\")\n", " .build();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `%classpath config resolver` gives you able to define custom repository" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%classpath config resolver repository.spring.snapshot http://repo.spring.io/snapshot" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%classpath add mvn org.springframework spring-context 5.0.3.BUILD-SNAPSHOT" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import and Unimport\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%import com.twosigma.beakerx.widgets.integers.IntSlider\n", "w = new IntSlider()\n", "w.value = 60\n", "w" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "w2 = new IntSlider()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%unimport com.twosigma.beakerx.widgets.integers.IntSlider" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "w3 = new IntSlider()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%import static java.lang.Math.PI" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "PI" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "More details of the implementation:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%classpath add jar ../resources/jar/demo.jar\n", "%classpath\n", "5+5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%classpath\n", "%classpath add jar ../resources/jar/demo.jar" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%classpath add jar ../resources/jar/demo.jar\n", "%classpath" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%import static com.example.Demo.staticTest" ] } ], "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": 4, "nbformat_minor": 1 }