Skip to content

Instantly share code, notes, and snippets.

@takawitter
Created June 13, 2013 23:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takawitter/5778410 to your computer and use it in GitHub Desktop.
Save takawitter/5778410 to your computer and use it in GitHub Desktop.
JavaからScalaを呼び出すコード。カレントディレクトリ直下のlibディレクトリに関連ライブラリ(Scala本体含む)が入っている前提。 Settingsのclasspathに利用するライブラリのjar(Scala自体のもの含む)を追加して、setByUserをtrueに。それをIMainに渡すことでクラスパスが通る。 変数bind時の出力を押さえるためにquietBindを使ってる。
package scalatest;
import java.io.File;
import scala.tools.nsc.Settings;
import scala.tools.nsc.interpreter.IMain;
import scala.tools.nsc.interpreter.NamedParamClass;
public class ScalaInvoke {
public static void main(String[] args) throws Exception{
Settings s = new Settings();
for(File p : new File("lib").listFiles()){
if(p.getName().endsWith(".jar"))
s.classpath().append(p.getAbsolutePath());
}
s.classpath().setByUser_$eq(true);
IMain m = new IMain(s);
m.quietBind(new NamedParamClass("n", "Int", 10));
m.eval("1 to n foreach { print(_) }");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment