scala.tools.nsc.Interpreter and classpath

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

scala.tools.nsc.Interpreter and classpath

Sanjay Dasgupta
I'm trying to use scala.tools.nsc.Interpreter to execute a script, but cant get it to recognize classes in jars specified on the command line (using scala -cp ....) of the host program. There also does not seem to be much  documentation on the use of Interpreter.

How do I get around this problem?

Any help appreciated.

- Sanjay
Reply | Threaded
Open this post in threaded view
|

Re: scala.tools.nsc.Interpreter and classpath

Michael Fortin
I've been wrestling with the same code too.  It looks something like:

var settings = new GenericRunnerSettings(…)
settings.classpath.append("your/path/to/your.jar")
var interpreter = new Interpreter(settings, out)

Your probably better off working with the InterpreterLoop class though.   Documentation is hard to find for it as well.
I had to dig through the source to figure it out.

Maybe this will help:
It's not finished, but maybe enough to get you started.

_Mike

On Sep 21, 2010, at 4:21 AM, Sanjay Dasgupta wrote:

I'm trying to use scala.tools.nsc.Interpreter to execute a script, but cant get it to recognize classes in jars specified on the command line (using scala -cp ....) of the host program. There also does not seem to be much  documentation on the use of Interpreter.

How do I get around this problem?

Any help appreciated.

- Sanjay

Reply | Threaded
Open this post in threaded view
|

Re: scala.tools.nsc.Interpreter and classpath

Sanjay Dasgupta
Hi Mike,

Thanks for the hints.

I also found these other links to more information on the subject:


- Sanjay

On Tue, Sep 21, 2010 at 8:46 PM, Michael Fortin <[hidden email]> wrote:
I've been wrestling with the same code too.  It looks something like:

var settings = new GenericRunnerSettings(…)
settings.classpath.append("your/path/to/your.jar")
var interpreter = new Interpreter(settings, out)

Your probably better off working with the InterpreterLoop class though.   Documentation is hard to find for it as well.
I had to dig through the source to figure it out.

Maybe this will help:
It's not finished, but maybe enough to get you started.

_Mike

On Sep 21, 2010, at 4:21 AM, Sanjay Dasgupta wrote:

I'm trying to use scala.tools.nsc.Interpreter to execute a script, but cant get it to recognize classes in jars specified on the command line (using scala -cp ....) of the host program. There also does not seem to be much  documentation on the use of Interpreter.

How do I get around this problem?

Any help appreciated.

- Sanjay


Reply | Threaded
Open this post in threaded view
|

Re: scala.tools.nsc.Interpreter and classpath

Paul Phillips-3
On Wed, Sep 22, 2010 at 10:01:29AM +0530, Sanjay Dasgupta wrote:
> Thanks for the hints.
>
> I also found these other links to more information on the subject:

One place people should look a lot more often when trying to figure out
something about the compiler is the test suite.  It's a pretty shabby
test suite, I'll give you that, but it doesn't take very long to look
and the nice thing about code in the test suite is that it almost has to
be up to date since the tests are apparently passing.

On this subject the first thing I find is this:

http://github.com/scala/scala/blob/master/test/files/jvm/interpreter.scala

With this relevant appearing main method:

  def main(args: Array[String]) {
    val input = new BufferedReader(new StringReader(testCodeString))
    val output = new PrintWriter(
      new Skip1Writer(new OutputStreamWriter(Console.out)))
    val repl = new InterpreterLoop(input, output)
    val settings = new Settings
    // when running that compiler, give it a scala-library to the classpath
    settings.classpath.value = System.getProperty("java.class.path")
    repl.main(settings)
    println()

    val interp = new Interpreter(settings)
    interp.interpret("def plusOne(x: Int) = x + 1")
    interp.interpret("plusOne(5)")
    interp.reset()
    interp.interpret("\"after reset\"")
    interp.interpret("plusOne(5) // should be undefined now")
  }

And there are more.

--
Paul Phillips      | Adultery is the application of democracy to love.
In Theory          |     -- H. L. Mencken
Empiricist         |
i pull his palp!   |----------* http://www.improving.org/paulp/ *----------
Reply | Threaded
Open this post in threaded view
|

Re: scala.tools.nsc.Interpreter and classpath

Sanjay Dasgupta
Hi Paul,

Thanks for the directions. I haven't started digging yet, but will soon.

I gave settings.classpath.value = System.getProperty("java.class.path") a quick shot (I'd been using interpSettings.usejavacp.value = true before), but I still can't get the embedded interpreter to recognize the jars defined with -cp ... on the host program's scala command line.

Thanks,

- Sanjay

On Wed, Sep 22, 2010 at 10:46 AM, Paul Phillips <[hidden email]> wrote:
On Wed, Sep 22, 2010 at 10:01:29AM +0530, Sanjay Dasgupta wrote:
> Thanks for the hints.
>
> I also found these other links to more information on the subject:

One place people should look a lot more often when trying to figure out
something about the compiler is the test suite.  It's a pretty shabby
test suite, I'll give you that, but it doesn't take very long to look
and the nice thing about code in the test suite is that it almost has to
be up to date since the tests are apparently passing.

On this subject the first thing I find is this:

http://github.com/scala/scala/blob/master/test/files/jvm/interpreter.scala

With this relevant appearing main method:

 def main(args: Array[String]) {
   val input = new BufferedReader(new StringReader(testCodeString))
   val output = new PrintWriter(
     new Skip1Writer(new OutputStreamWriter(Console.out)))
   val repl = new InterpreterLoop(input, output)
   val settings = new Settings
   // when running that compiler, give it a scala-library to the classpath
   settings.classpath.value = System.getProperty("java.class.path")
   repl.main(settings)
   println()

   val interp = new Interpreter(settings)
   interp.interpret("def plusOne(x: Int) = x + 1")
   interp.interpret("plusOne(5)")
   interp.reset()
   interp.interpret("\"after reset\"")
   interp.interpret("plusOne(5) // should be undefined now")
 }

And there are more.

--
Paul Phillips      | Adultery is the application of democracy to love.
In Theory          |     -- H. L. Mencken
Empiricist         |
i pull his palp!   |----------* http://www.improving.org/paulp/ *----------

Reply | Threaded
Open this post in threaded view
|

Re: scala.tools.nsc.Interpreter and classpath

Razvan Cojocaru-2
In reply to this post by Sanjay Dasgupta
In the settings, there's a usejavacp you need to set to true...can't
send exact code now

On 9/21/10, Sanjay Dasgupta <[hidden email]> wrote:

> I'm trying to use scala.tools.nsc.Interpreter to execute a script, but cant
> get it to recognize classes in jars specified on the command line (using
> scala -cp ....) of the host program. There also does not seem to be
> much  documentation on the use of Interpreter.
>
> How do I get around this problem?
>
> Any help appreciated.
>
> - Sanjay
>

--
Sent from my mobile device
Razvan Cojocaru,
Work: http://www.sigma-systems.com
Playground: http://wiki.homecloud.ca
Latest cool toys: Scripster and Gremlins
Follow me: RSS Feed, Twitter, GitHub.
Reply | Threaded
Open this post in threaded view
|

Re: scala.tools.nsc.Interpreter and classpath

Sanjay Dasgupta
Hi Razvan,

I've tried that too, but seem to have no luck. The usejavacp option does help it pick up the standard Scala libraries, but any additional jar files specified on the host program's command line (with -cp ...) are still not recognized.

Thanks,

- Sanjay



On Wed, Sep 22, 2010 at 7:58 PM, Razvan (Pub) Cojocaru <[hidden email]> wrote:
In the settings, there's a usejavacp you need to set to true...can't
send exact code now

On 9/21/10, Sanjay Dasgupta <[hidden email]> wrote:
> I'm trying to use scala.tools.nsc.Interpreter to execute a script, but cant
> get it to recognize classes in jars specified on the command line (using
> scala -cp ....) of the host program. There also does not seem to be
> much  documentation on the use of Interpreter.
>
> How do I get around this problem?
>
> Any help appreciated.
>
> - Sanjay
>

--
Sent from my mobile device

Reply | Threaded
Open this post in threaded view
|

Re: scala.tools.nsc.Interpreter and classpath

Chris Twiner

You must also set the bootclasspath.value to a path that contains the compiler and library jars.

On Sep 22, 2010 5:02 PM, "Sanjay Dasgupta" <[hidden email]> wrote:

Hi Razvan,

I've tried that too, but seem to have no luck. The usejavacp option does help it pick up the standard Scala libraries, but any additional jar files specified on the host program's command line (with -cp ...) are still not recognized.

Thanks,

- Sanjay

On Wed, Sep 22, 2010 at 7:58 PM, Razvan (Pub) Cojocaru <[hidden email]> wrote: > > In the settings,...

Reply | Threaded
Open this post in threaded view
|

RE: scala.tools.nsc.Interpreter and classpath

Razvan Cojocaru-2
In reply to this post by Sanjay Dasgupta

I’m sure it works…the way  I use it in the scripster:

http://github.com/razie/scripster/blob/master/src/razie/base/scripting/ScriptScala.scala#L56

 

maybe you’re using other classes or setups, my basic sequence is:

 

razie.Debug ("Scripster using java classpath")

        val env = new nsc.Settings(errLogger)

        env.usejavacp.value = true

 

val p =        new Interpreter(env)

 

    p.setContextClassLoader

    

. . . .

   val r = p.evalExpr[Any](script)

 

 

-          Maybe you should look at using the scripster, it does other stuff as well, like binding variables you have in a context before running the script and extracting the new values defined…

This is proof that it works: http://bit.ly/aaSjW0 click “try now” and evaluate each line one by one with F9…the demo scripster server simply put scalaz library in the classpath and used the code above to create the parser…

 

Examples to use the scripster are in its junit tests: http://github.com/razie/scripster/blob/master/test_src/razie/scripster/test/ScalaScriptTest.scala

 

Here’s one:

 

def testbind = expect ("12") { 
    // test binding variables
    ScriptScala ("a+b").eval (new ScalaScriptContext(null, "a", "1", "b", "2")) getOrElse "?"
    }

 

Cheers – download the scripster pre-built here: http://code.google.com/p/razpub/downloads/list

 

From: Sanjay Dasgupta [mailto:[hidden email]]
Sent: Wednesday, September 22, 2010 11:02 AM
To: Razvan (Pub) Cojocaru
Cc: [hidden email]
Subject: Re: [scala-user] scala.tools.nsc.Interpreter and classpath

 

Hi Razvan,

 

I've tried that too, but seem to have no luck. The usejavacp option does help it pick up the standard Scala libraries, but any additional jar files specified on the host program's command line (with -cp ...) are still not recognized.

 

Thanks,

 

- Sanjay

 

On Wed, Sep 22, 2010 at 7:58 PM, Razvan (Pub) Cojocaru <[hidden email]> wrote:

In the settings, there's a usejavacp you need to set to true...can't
send exact code now


On 9/21/10, Sanjay Dasgupta <[hidden email]> wrote:


> I'm trying to use scala.tools.nsc.Interpreter to execute a script, but cant
> get it to recognize classes in jars specified on the command line (using
> scala -cp ....) of the host program. There also does not seem to be
> much  documentation on the use of Interpreter.
>
> How do I get around this problem?
>
> Any help appreciated.
>
> - Sanjay
>

--

Sent from my mobile device

 

Razvan Cojocaru,
Work: http://www.sigma-systems.com
Playground: http://wiki.homecloud.ca
Latest cool toys: Scripster and Gremlins
Follow me: RSS Feed, Twitter, GitHub.