This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Embedding the Scala 2.8 interpreter

14 replies
Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.

hi,

i want to embed a scala 2.8 REPL in my (swing-based) app.

i have tried this very quick approach:

import scala.tools.nsc.{ InterpreterLoop, Settings }
import java.awt._
import java.awt.event._
import java.io._
import javax.swing._

class MyFrame extends JFrame {
val cp = getContentPane
val ggSplit = new JSplitPane( JSplitPane.VERTICAL_SPLIT )
val ggInput = new JTextArea( 6, 40 )
ggSplit.setTopComponent( ggInput )
cp.add( ggSplit, BorderLayout.CENTER )

val w = new PrintWriter( System.out )
val pipe = new PipedOutputStream()
val r = new BufferedReader( new InputStreamReader( new PipedInputStream( pipe )))
val ps = new PrintStream( pipe )

val imap = ggInput.getInputMap( JComponent.WHEN_FOCUSED )
val amap = ggInput.getActionMap()
imap.put( KeyStroke.getKeyStroke( KeyEvent.VK_E, InputEvent.META_MASK, "exec" )
amap.put( "exec", new AbstractAction {
def actionPerformed( e: ActionEvent ) {
val txt = ggInput.getSelectedText()
if( txt != null ) {
ps.print( txt )
ps.flush()
}
}
})

val repl = new InterpreterLoop( r, w )
val settings = new Settings()

setSize( 200, 200 )
setVisible( true )

repl.main( settings )
}

and scala-library and scala-compiler are on my classpath. the welcome message is printed, but then:

Exception in thread "AWT-EventQueue-0" scala.tools.nsc.MissingRequirementError: object scala not found.
at scala.tools.nsc.symtab.Definitions$definitions$.getModuleOrClass(Definitions.scala:514)
at scala.tools.nsc.symtab.Definitions$definitions$.ScalaPackage(Definitions.scala:43)
at scala.tools.nsc.symtab.Definitions$definitions$.ScalaPackageClass(Definitions.scala:44)
at scala.tools.nsc.symtab.Definitions$definitions$.UnitClass(Definitions.scala:86)
at scala.tools.nsc.symtab.Definitions$definitions$.init(Definitions.scala:770)
at scala.tools.nsc.Global$Run.(Global.scala:620)
at scala.tools.nsc.Interpreter.compileSources(Interpreter.scala:399)
at scala.tools.nsc.Interpreter.compileString(Interpreter.scala:407)
at scala.tools.nsc.InterpreterLoop$$anonfun$bindSettings$1.apply(InterpreterLoop.scala:131)
at scala.tools.nsc.InterpreterLoop$$anonfun$bindSettings$1.apply(InterpreterLoop.scala:130)
at scala.tools.nsc.Interpreter$$anonfun$beQuietDuring$2.apply(Interpreter.scala:102)
at scala.util.control.Exception$Catch.apply(Exception.scala:79)
at scala.tools.nsc.Interpreter.beQuietDuring(Interpreter.scala:100)
at scala.tools.nsc.InterpreterLoop.bindSettings(InterpreterLoop.scala:130)
at scala.tools.nsc.InterpreterLoop.repl(InterpreterLoop.scala:262)
at scala.tools.nsc.InterpreterLoop.main(InterpreterLoop.scala:439)
at de.sciss.kontur.gui.ScalaInterpreterFrame.(ScalaInterpreterFrame.scala:60)
...

so i must be missing something....

also my question, is there a tutorial for embedding the 2.8 REPL? and is this the best way, or should i use Interpreter directly? because

- i want the interpreter to use the current VM, i.e. the user should be able to access my application objects
- i don't want a single line input, but the user selects a text block and hits a special execution key
- i don't want the "scala>" prompt in the output window

thanks, -sciss-

Michael
Joined: 2009-02-23,
User offline. Last seen 20 weeks 1 day ago.
Re: Embedding the Scala 2.8 interpreter

Sciss writes:

> and scala-library and scala-compiler are on my classpath. the welcome message
is printed, but then:
>
> Exception in thread "AWT-EventQueue-0"
scala.tools.nsc.MissingRequirementError: object scala not found.

From looking at Settings.scala it seems to me the classpath is only picked up
from the CLASSPATH environment variable:

protected def classpathDefault =
sysenvopt("CLASSPATH") getOrElse "."

So specifying -cp on the command line seems not enough.

Michael

Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 4 days ago.
Re: Re: Embedding the Scala 2.8 interpreter
This was a known issue on windows in the past, can you confirm what OS you're using?
I'm sure I submitted a patch somewhere...


On 3 February 2010 10:21, michid <michid [at] gmail [dot] com> wrote:
Sciss <contact@...> writes:

> and scala-library and scala-compiler are on my classpath. the welcome message
is printed, but then:
>
> Exception in thread "AWT-EventQueue-0"
scala.tools.nsc.MissingRequirementError: object scala not found.

From looking at Settings.scala it seems to me the classpath is only picked up
from the CLASSPATH environment variable:

 protected def classpathDefault =
   sysenvopt("CLASSPATH") getOrElse "."

So specifying -cp on the command line seems not enough.

Michael




--
Kevin Wright

mail/google talk: kev [dot] lee [dot] wright [at] googlemail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda

Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: Re: Embedding the Scala 2.8 interpreter

I encountered the same thing recently on Mac OS X. I thought it was
related to the changes in classpath propagation [1] Scala 2.8.

I worked around the problem by explicitly passing [2] the value of the
system property 'java.class.path' to the Settings used by the
interpreter.

-jason

[1] http://old.nabble.com/-scala--recent-changes-in-2.8-nightly-classpath-ma...
[2] http://gist.github.com/290632

On Wed, Feb 3, 2010 at 11:32 AM, Kevin Wright
wrote:
> This was a known issue on windows in the past, can you confirm what OS
> you're using?
> I'm sure I submitted a patch somewhere...
>
>
> On 3 February 2010 10:21, michid wrote:
>>
>> Sciss writes:
>>
>> > and scala-library and scala-compiler are on my classpath. the welcome
>> > message
>> is printed, but then:
>> >
>> > Exception in thread "AWT-EventQueue-0"
>> scala.tools.nsc.MissingRequirementError: object scala not found.
>>
>> From looking at Settings.scala it seems to me the classpath is only picked
>> up
>> from the CLASSPATH environment variable:
>>
>>  protected def classpathDefault =
>>    sysenvopt("CLASSPATH") getOrElse "."
>>
>> So specifying -cp on the command line seems not enough.
>>
>> Michael
>>
>
>
>
> --
> Kevin Wright
>
> mail/google talk: kev [dot] lee [dot] wright [at] googlemail [dot] com
> wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
> skype: kev.lee.wright
> twitter: @thecoda
>
>

Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 4 days ago.
Re: Re: Embedding the Scala 2.8 interpreter
Okay... I'm now on OSX as well.
It *should* be using the same bash scripts that PaulP is maintaining (it least, he was the last time this first came up - no idea if that baton has been handed over at all) I'll take a look and see if anything's changed, for completeness' sake I can also check back over the windows scripts.
Fingers crossed this is sortable with just a simple script update :)


On 3 February 2010 11:54, Jason Zaugg <jzaugg [at] gmail [dot] com> wrote:
I encountered the same thing recently on Mac OS X. I thought it was
related to the changes  in classpath propagation [1] Scala 2.8.

I worked around the problem by explicitly passing [2] the value of the
system property 'java.class.path' to the Settings used by the
interpreter.

-jason

[1] http://old.nabble.com/-scala--recent-changes-in-2.8-nightly-classpath-management-td26233977.html
[2] http://gist.github.com/290632

On Wed, Feb 3, 2010 at 11:32 AM, Kevin Wright
<kev [dot] lee [dot] wright [at] googlemail [dot] com> wrote:
> This was a known issue on windows in the past, can you confirm what OS
> you're using?
> I'm sure I submitted a patch somewhere...
>
>
> On 3 February 2010 10:21, michid <michid [at] gmail [dot] com> wrote:
>>
>> Sciss <contact@...> writes:
>>
>> > and scala-library and scala-compiler are on my classpath. the welcome
>> > message
>> is printed, but then:
>> >
>> > Exception in thread "AWT-EventQueue-0"
>> scala.tools.nsc.MissingRequirementError: object scala not found.
>>
>> From looking at Settings.scala it seems to me the classpath is only picked
>> up
>> from the CLASSPATH environment variable:
>>
>>  protected def classpathDefault =
>>    sysenvopt("CLASSPATH") getOrElse "."
>>
>> So specifying -cp on the command line seems not enough.
>>
>> Michael
>>
>
>
>
> --
> Kevin Wright
>
> mail/google talk: kev [dot] lee [dot] wright [at] googlemail [dot] com
> wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
> skype: kev.lee.wright
> twitter: @thecoda
>
>



--
Kevin Wright

mail/google talk: kev [dot] lee [dot] wright [at] googlemail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda

Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: Re: Embedding the Scala 2.8 interpreter

i am on os x 10.6.2

$ java -version
java version "1.6.0_17"
Java(TM) SE Runtime Environment (build 1.6.0_17-b04-248-10M3025)
Java HotSpot(TM) Client VM (build 14.3-b01-101, mixed mode)

i found an older post ( http://old.nabble.com/-scala--recent-changes-in-2.8-nightly-classpath-ma... ) that suggested to do

settings.classpath.value = System.getProperty("java.class.path")

but when i do that the application just hangs completely....

ciao, -sciss-

Am 03.02.2010 um 10:32 schrieb Kevin Wright:

> This was a known issue on windows in the past, can you confirm what OS you're using?
>
> I'm sure I submitted a patch somewhere...
>
>
>
> On 3 February 2010 10:21, michid wrote:
> Sciss writes:
>
>> and scala-library and scala-compiler are on my classpath. the welcome message
> is printed, but then:
>>
>> Exception in thread "AWT-EventQueue-0"
> scala.tools.nsc.MissingRequirementError: object scala not found.
>
> From looking at Settings.scala it seems to me the classpath is only picked up
> from the CLASSPATH environment variable:
>
> protected def classpathDefault =
> sysenvopt("CLASSPATH") getOrElse "."
>
> So specifying -cp on the command line seems not enough.
>
> Michael
>
>
>
>

Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: Re: Embedding the Scala 2.8 interpreter

I encountered the problem within a java.exe launched by IntelliJ that
had scala-library and scala-compiler included in -classpath. No bash
scripts were involved.

-jason

On Wed, Feb 3, 2010 at 1:44 PM, Kevin Wright
wrote:
> Okay... I'm now on OSX as well.
> It *should* be using the same bash scripts that PaulP is maintaining (it
> least, he was the last time this first came up - no idea if that baton has
> been handed over at all)
> I'll take a look and see if anything's changed, for completeness' sake I can
> also check back over the windows scripts.

Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 4 days ago.
Re: Re: Embedding the Scala 2.8 interpreter
What main class did you specify?
I believe that the repl operates via a nested classloader, and that the classpath for *this* is supplied via command-line properties when launched via the shell script.  In part, this helps work around argument length limitations in certain operating systems by internally passing the classpath via a file.
Your best bet right now is to take a look at how the bash script does it, then copy that :)

I discovered all of this in the past when... wait for it... trying to implement a REPL in swing. That one went off at a bit of a tangent, and led to me wanting to migrate the entire javax.swing.text hierarchy over to scala.swing.text
The effort is still in progress, partly because I disliked the amount of boilerplate in delegating scala-style properties to javabean properties.  That, in turn, led me further down the rabbit hole and thus the auto-proxy plugin was born.
Now that 2.8 beta is less of a moving target I hope to get the autoproxy-plugin finished and working with java<->scala property delegation, then the scala.swing.text stuff should be much less painful, and then I can - finally - finish the REPL!
Hopefully the stack will be unrolling any day now...


On 3 February 2010 13:01, Jason Zaugg <jzaugg [at] gmail [dot] com> wrote:
I encountered the problem within a java.exe launched by IntelliJ that
had scala-library and scala-compiler included in -classpath. No bash
scripts were involved.

-jason

On Wed, Feb 3, 2010 at 1:44 PM, Kevin Wright
<kev [dot] lee [dot] wright [at] googlemail [dot] com> wrote:
> Okay... I'm now on OSX as well.
> It *should* be using the same bash scripts that PaulP is maintaining (it
> least, he was the last time this first came up - no idea if that baton has
> been handed over at all)
> I'll take a look and see if anything's changed, for completeness' sake I can
> also check back over the windows scripts.



--
Kevin Wright

mail/google talk: kev [dot] lee [dot] wright [at] googlemail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda

Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: Re: Embedding the Scala 2.8 interpreter

On Wed, Feb 3, 2010 at 2:20 PM, Kevin Wright
wrote:
> What main class did you specify?
> I believe that the repl operates via a nested classloader, and that the
> classpath for *this* is supplied via command-line properties when launched
> via the shell script.  In part, this helps work around argument length
> limitations in certain operating systems by internally passing the classpath
> via a file.
> Your best bet right now is to take a look at how the bash script does it,
> then copy that :)

The main class was an IntelliJ test runner wrapping up Specs. I was
sick trying to use a Java debugger with Scala (which involves a mental
process of demangling, implicit outer-ing, lamda lowering etc). So I
tried to use Interpreter.break from within my code.

> I discovered all of this in the past when... wait for it... trying to
> implement a REPL in swing.

As it turns out the IntelliJ unit test console can't accept input, so
I gave up. A Swing REPL on the other hand would avoid such
limitations... :)

> Hopefully the stack will be unrolling any day now...

I've been waiting for that day, too!

-jason

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Re: Embedding the Scala 2.8 interpreter

By pure coincidence I was working on this much of the day yesterday.
Something since 2.7 has made the interpreter a lot harder to embed, and
I haven't quite pinned it down yet, but I think I was close yesterday so
maybe today. In the end I expect the process will be much more
straightforward.

Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 4 days ago.
Re: Re: Embedding the Scala 2.8 interpreter
Can we have a way to supply an alternative to jline for handling the tab completion?please...


On 3 February 2010 16:07, Paul Phillips <paulp [at] improving [dot] org> wrote:
By pure coincidence I was working on this much of the day yesterday.
Something since 2.7 has made the interpreter a lot harder to embed, and
I haven't quite pinned it down yet, but I think I was close yesterday so
maybe today.  In the end I expect the process will be much more
straightforward.

--
Paul Phillips      | Every election is a sort of advance auction sale
Vivid              | of stolen goods.
Empiricist         |     -- H. L. Mencken
slap pi uphill!    |----------* http://www.improving.org/paulp/ *----------



--
Kevin Wright

mail/google talk: kev [dot] lee [dot] wright [at] googlemail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Re: Embedding the Scala 2.8 interpreter

On Wed, Feb 03, 2010 at 04:11:25PM +0000, Kevin Wright wrote:
> Can we have a way to supply an alternative to jline for handling the
> tab completion?

Completion is a property of the reader, and it's already abstracted. I
could separate things more but if there's something you can't do now you
will have to be specific.

Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: Re: Embedding the Scala 2.8 interpreter

ok, great, i'm looking forward to it!

Am 03.02.2010 um 16:07 schrieb Paul Phillips:

> By pure coincidence I was working on this much of the day yesterday.
> Something since 2.7 has made the interpreter a lot harder to embed, and
> I haven't quite pinned it down yet, but I think I was close yesterday so
> maybe today. In the end I expect the process will be much more
> straightforward.
>

Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: Re: Embedding the Scala 2.8 interpreter

this seems to fix the problem:

System.setProperty( "scala.home", "/Users/rutz/Documents/devel/scala-2.8.0-snapshot" ) // or whatever is your path

...

maybe that should be written in the Interpreter(Loop) scaladoc!

ciao, -sciss-

Am 03.02.2010 um 13:00 schrieb Sciss:

> i am on os x 10.6.2
>
> $ java -version
> java version "1.6.0_17"
> Java(TM) SE Runtime Environment (build 1.6.0_17-b04-248-10M3025)
> Java HotSpot(TM) Client VM (build 14.3-b01-101, mixed mode)
>
>
> i found an older post ( http://old.nabble.com/-scala--recent-changes-in-2.8-nightly-classpath-ma... ) that suggested to do
>
> settings.classpath.value = System.getProperty("java.class.path")
>
> but when i do that the application just hangs completely....
>
> ciao, -sciss-
>
>
> Am 03.02.2010 um 10:32 schrieb Kevin Wright:
>
>> This was a known issue on windows in the past, can you confirm what OS you're using?
>>
>> I'm sure I submitted a patch somewhere...
>>
>>
>>
>> On 3 February 2010 10:21, michid wrote:
>> Sciss writes:
>>
>>> and scala-library and scala-compiler are on my classpath. the welcome message
>> is printed, but then:
>>>
>>> Exception in thread "AWT-EventQueue-0"
>> scala.tools.nsc.MissingRequirementError: object scala not found.
>>
>> From looking at Settings.scala it seems to me the classpath is only picked up
>> from the CLASSPATH environment variable:
>>
>> protected def classpathDefault =
>> sysenvopt("CLASSPATH") getOrElse "."
>>
>> So specifying -cp on the command line seems not enough.
>>
>> Michael
>>
>>
>>
>>

Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: Embedding the Scala 2.8 interpreter

hi,

just wanted to point out that i made a little self contained swing view for the interpreter, might be useful (i know there's other similar projects):

http://github.com/Sciss/ScalaInterpreterPane

ciao, -sciss-

Am 03.02.2010 um 01:38 schrieb Sciss:

> hi,
>
> i want to embed a scala 2.8 REPL in my (swing-based) app.
>
> i have tried this very quick approach:
>
> import scala.tools.nsc.{ InterpreterLoop, Settings }
> import java.awt._
> import java.awt.event._
> import java.io._
> import javax.swing._
>
> class MyFrame extends JFrame {
> val cp = getContentPane
> val ggSplit = new JSplitPane( JSplitPane.VERTICAL_SPLIT )
> val ggInput = new JTextArea( 6, 40 )
> ggSplit.setTopComponent( ggInput )
> cp.add( ggSplit, BorderLayout.CENTER )
>
> val w = new PrintWriter( System.out )
> val pipe = new PipedOutputStream()
> val r = new BufferedReader( new InputStreamReader( new PipedInputStream( pipe )))
> val ps = new PrintStream( pipe )
>
> val imap = ggInput.getInputMap( JComponent.WHEN_FOCUSED )
> val amap = ggInput.getActionMap()
> imap.put( KeyStroke.getKeyStroke( KeyEvent.VK_E, InputEvent.META_MASK, "exec" )
> amap.put( "exec", new AbstractAction {
> def actionPerformed( e: ActionEvent ) {
> val txt = ggInput.getSelectedText()
> if( txt != null ) {
> ps.print( txt )
> ps.flush()
> }
> }
> })
>
> val repl = new InterpreterLoop( r, w )
> val settings = new Settings()
>
> setSize( 200, 200 )
> setVisible( true )
>
> repl.main( settings )
> }
>
> and scala-library and scala-compiler are on my classpath. the welcome message is printed, but then:
>
> Exception in thread "AWT-EventQueue-0" scala.tools.nsc.MissingRequirementError: object scala not found.
> at scala.tools.nsc.symtab.Definitions$definitions$.getModuleOrClass(Definitions.scala:514)
> at scala.tools.nsc.symtab.Definitions$definitions$.ScalaPackage(Definitions.scala:43)
> at scala.tools.nsc.symtab.Definitions$definitions$.ScalaPackageClass(Definitions.scala:44)
> at scala.tools.nsc.symtab.Definitions$definitions$.UnitClass(Definitions.scala:86)
> at scala.tools.nsc.symtab.Definitions$definitions$.init(Definitions.scala:770)
> at scala.tools.nsc.Global$Run.(Global.scala:620)
> at scala.tools.nsc.Interpreter.compileSources(Interpreter.scala:399)
> at scala.tools.nsc.Interpreter.compileString(Interpreter.scala:407)
> at scala.tools.nsc.InterpreterLoop$$anonfun$bindSettings$1.apply(InterpreterLoop.scala:131)
> at scala.tools.nsc.InterpreterLoop$$anonfun$bindSettings$1.apply(InterpreterLoop.scala:130)
> at scala.tools.nsc.Interpreter$$anonfun$beQuietDuring$2.apply(Interpreter.scala:102)
> at scala.util.control.Exception$Catch.apply(Exception.scala:79)
> at scala.tools.nsc.Interpreter.beQuietDuring(Interpreter.scala:100)
> at scala.tools.nsc.InterpreterLoop.bindSettings(InterpreterLoop.scala:130)
> at scala.tools.nsc.InterpreterLoop.repl(InterpreterLoop.scala:262)
> at scala.tools.nsc.InterpreterLoop.main(InterpreterLoop.scala:439)
> at de.sciss.kontur.gui.ScalaInterpreterFrame.(ScalaInterpreterFrame.scala:60)
> ...
>
> so i must be missing something....
>
> also my question, is there a tutorial for embedding the 2.8 REPL? and is this the best way, or should i use Interpreter directly? because
>
> - i want the interpreter to use the current VM, i.e. the user should be able to access my application objects
> - i don't want a single line input, but the user selects a text block and hits a special execution key
> - i don't want the "scala>" prompt in the output window
>
>
> thanks, -sciss-
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland