Run a Java program in “Google Colaboratory”
モチベーション
“Google Colaboratory”でjs、C言語実行できて、Javaも考えた。
Googleしたら、Javaは可能だ。
日本語化したJava Kernelノートブックは共有した。
使い方:
このノートブックは、Google ColaboratoryでJavaを使用するためのテンプレートです。
JupyterカーネルとしてIJavaを使用します。
- 以下のセルを実行し(クリックしてShift + Enterキーを押す)、
- その後、ページを更新します(F5キーを押す)。
これで、Javaコードを記述できます。
実行例
表示例:
Prepare Java Kernel for Google Colab
This notebook is a template to use Java in Google Colaboratory (inspired by Google Colab Javascript QuickStart). It uses IJava as the Jupyter Kernel.
- Run the cell bellow (click it and press
Shift
+Enter
), - then refresh the page (press
F5
) right after that.
Now you can write Java code. See examples in the next section.
このノートブックは、Google ColaboratoryでJavaを使用するためのテンプレートです。 JupyterカーネルとしてIJavaを使用します。
- 以下のセルを実行し(クリックしてShift + Enterキーを押す)、
- その後、ページを更新します(F5キーを押す)。
これで、Javaコードを記述できます。 次のセクションの例を参照してください。
!apt update -q
!apt-get install -q openjdk-11-jdk-headless
!curl -L https://github.com/SpencerPark/IJava/releases/download/v1.3.0/ijava-1.3.0.zip -o ijava-kernel.zip
!unzip -q ijava-kernel.zip -d ijava-kernel && cd ijava-kernel && python3 install.py --sys-prefix
!jupyter kernelspec list
Get:1 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease [3,626 B] Hit:2 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 InRelease Ign:3 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 InRelease Hit:4 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 Release Hit:5 http://archive.ubuntu.com/ubuntu bionic InRelease Get:6 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic InRelease [15.9 kB] Get:7 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB] Get:8 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB] Get:9 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB] Hit:10 http://ppa.launchpad.net/cran/libgit2/ubuntu bionic InRelease Hit:11 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease Hit:13 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic InRelease Get:14 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [2,272 kB] Get:15 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic/main Sources [1,957 kB] Get:16 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [3,168 kB] Get:17 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [1,498 kB] Get:18 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic/main amd64 Packages [1,003 kB] Get:19 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [2,734 kB] Fetched 12.9 MB in 4s (3,132 kB/s) Reading package lists... Building dependency tree... Reading state information... 52 packages can be upgraded. Run 'apt list --upgradable' to see them. Reading package lists... Building dependency tree... Reading state information... openjdk-11-jdk-headless is already the newest version (11.0.15+10-0ubuntu0.18.04.1). The following packages were automatically installed and are no longer required: libnvidia-common-460 nsight-compute-2020.2.0 Use 'apt autoremove' to remove them. 0 upgraded, 0 newly installed, 0 to remove and 52 not upgraded. % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 100 3287k 100 3287k 0 0 2369k 0 0:00:01 0:00:01 --:--:-- 7679k install.py:169: DeprecationWarning: replace is ignored. Installing a kernelspec always replaces an existing installation replace=args.replace Installed java kernel into "/usr/share/jupyter/kernels/java" Available kernels: ir /usr/local/share/jupyter/kernels/ir python3 /usr/local/share/jupyter/kernels/python3 java /usr/share/jupyter/kernels/java
Examples
System.out.println("Hello world!");
Hello world!
%maven org.knowm.xchart:xchart:3.5.2
import org.knowm.xchart.*;
double[] xData = new double[] { 0.0, 1.0, 2.0 };
double[] yData = new double[] { 2.0, 1.0, 0.0 };
XYChart chart = QuickChart.getChart("Sample Chart", "X", "Y", "y(x)", xData, yData);
BitmapEncoder.getBufferedImage(chart);