Created
June 13, 2013 23:51
-
-
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を使ってる。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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