Java
jshell
REPLは非公式なものとJava9で搭載予定のjshellがある。java9-eaをインストールしてjshellを試す。
./jp/ashitani/Sample.classに、jp.ashitani.Sampleパッケージ内の、hogehoge()メソッドを持つSampleクラスがいる前提で、
$ jshell jshell> import jp.ashitani.Sample.Sample jshell> Sample s = new Sample(); jshell> s.hogehoge(); hogehoge
jpype
pythonからJVM経由でJavaとつなぐ。本家。ドキュメント
OSXでは、setup.pyでJAVA_HOMEを決め打ちにしていたので環境変数を読むように修正。さらに このへんの設定をしないと動かず。
from jpype import * startJVM(getDefaultJVMPath(), "-ea") Sample=JPackage("jp.ashitani.Sample").Sample s= Sample() s.hogehoge() shutdownJVM()
すごいけどなんかキモい。三行目で、JavaのクラスをPythonのクラスでラッピングしたようなSampleというPythonクラスが爆誕する。
numpyのarrayを渡せるらしい。やってみよう。
package jp.ashitani.Sample; public class Sample{ public void hogehoge(){ System.out.println("hogehoge"); } public void printArray(int[] arg){ for (int i=0; i<arg.length; i++){ System.out.println(arg[i]); } } }
from jpype import * import numpy as np startJVM(getDefaultJVMPath(), "-ea") java.lang.System.out.println("hello world") Sample=JPackage("jp.ashitani.Sample").Sample s= Sample() s.hogehoge() a=np.array([1,2,3,4,5]) s.printArray(a) shutdownJVM()
きんもーっ☆なにがきもいって、ipython とかでs.priあたりでTABを押すとちゃんとprintArrayが出るあたり。