7

Based on a thread and the Scala interpreter sources it looks like it would not be that hard to get the equivalent of

def runLine(line: String): String

but has someone written this already?

Owen
  • 38,836
  • 14
  • 95
  • 125
  • 1
    possible duplicate of [Launch Scala REPL programatically?](http://stackoverflow.com/questions/2456868/launch-scala-repl-programatically) – Daniel C. Sobral Sep 02 '11 at 20:07
  • 1
    This answer gives more detail: http://stackoverflow.com/questions/2160355/drop-into-interpreter-during-arbitrary-scala-code-location . Note that the library has interface has changed (refactored) as of Scala 2.9. Also, you might run into problems with the classpath of the embedded REPL not being the same as the parent process; some solutions here: http://stackoverflow.com/questions/4121567/embedded-scala-repl-inherits-parent-classpath – Kipton Barros Sep 02 '11 at 20:40
  • @Daniel that's attempting to do a quite different thing, which is interrupt a scala program to start the usual REPL. I'm trying to duplicate the behavior of the REPL in a non-REPL. – Owen Sep 02 '11 at 23:26
  • @Owen [This one](http://stackoverflow.com/questions/3905948/how-do-i-embed-the-scala-2-8-interpreter-in-a-java-application), then? – Daniel C. Sobral Sep 03 '11 at 14:37
  • @Daniel That's pretty close, though it uses `interpreter.main(settings)`, so it's still using the standard REPL's main loop. – Owen Sep 03 '11 at 16:40
  • @Owen Well, there's [this](http://stackoverflow.com/questions/5348825/scala-tools-nsc-interpreter-how-to-execute-interpreter-statements-so-that-the), though it specifies 2.7.7. Not sure if it still works. It's also not a duplicate of the question -- the guy already had it figured out. [This other](http://stackoverflow.com/questions/2752206/dynamically-create-class-in-scala-should-i-use-interpreter) actually compiles and loads. – Daniel C. Sobral Sep 04 '11 at 01:39

1 Answers1

6

There are many examples floating out there, but they may be hard to find. Here is the one I wrote a while back:

http://code.google.com/p/simplex3d/source/browse/trunk/Simplex3dConsole/src/simplex3d/console/SimpleInterpreter.scala?r=790

The tricky part is to get the interpreter to load Scala jars. I have loaded jars manually to make it work with web-start, but in your case, simple settings.usejavacp.value = true may suffice.

You can see the interpreter in action here: http://www.simplex3d.org/console/

Lex
  • 1,184
  • 5
  • 11