I am working on a Java program that takes a Scala program in a text file, compiles it (inside Java), then runs it (also inside Java). I could not figure out a clean way to implement this. To compile, I tried to use code as shown below:
import scala.collection.JavaConversions;
import scala.tools.nsc.Global;
import scala.tools.nsc.Settings;
Global g = new Global(new Settings());
Global.Run run = g.new Run();
List<String> fileNames = new ArrayList<String>(Arrays.asList("Hello.scala"));
run.compile(JavaConversions.asScalaBuffer(fileNames).toList());
However, I get an error (reduced for clarity):
error: error while loading Object, Missing dependency 'object scala in compiler mirror', required by C:\Program Files\Java\jdk1.7.0_79\jre\lib\rt.jar(java/lang/Object.class)
I did not understand what was causing this or how to fix. As a temporary workaround, I tried compiling the Scala code externally and calling that in Java via:
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("java -cp scala-library.jar;. Hello");
While I think the code runs, it is not interactive with the Java program as I would like. For example, when running the Scala program (inside Java) if the Scala program wanted the user to type a string into the console, the Java program should do the same essentially.
Any guidance is appreciated.