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

IMain classloader

4 replies
cquiroz
Joined: 2011-12-09,
User offline. Last seen 42 years 45 weeks ago.

Hi all

I've been trying to integrate a scala interpreter into an application
running in OSGi.
I have a bundle that will crean an IMain instance and I'd like to use
all classes I have access in the local Runner class but after many
attempts I have failed to do so
I'm using the following code:

val settings = new Settings()
settings.embeddedDefaults(classOf[Runner].getClassLoader)

val i = new IMain(settings) {
override protected def parentClassLoader: ClassLoader =
classOf[Runner].getClassLoader
}
i.setContextClassLoader()
i.addImports("edu.gemini.aspen.giapi.status.StatusItem")

However the import fails with:
[packageobjects in 0ms]
:7: error: not found: value edu
import edu.gemini.aspen.giapi.status
^

Notice that if I do

println(classOf[Runner].getClassLoader.loadClass("edu.gemini.aspen.giapi.status.StatusItem"))
That works fine

So, it seems IMain is not delegating to the parentClassLoader.
Is there some way to force IMain to use the given classloader?

Regards
Carlos

Lex
Joined: 2010-02-28,
User offline. Last seen 42 years 45 weeks ago.
Re: IMain classloader
I have had this issue recently. The solution that worked for me was:

val imain = new IMain(settings, flusher) {
  override protected def parentClassLoader: ClassLoader = this.getClass.getClassLoader()
}

You can put whatever classloader you want instead of this.getClass.getClassLoader.


On Fri, Dec 9, 2011 at 8:23 AM, cquiroz <carlos.m.quiroz@gmail.com> wrote:
Hi all

I've been trying to integrate a scala interpreter into an application
running in OSGi.
I have a bundle that will crean an IMain instance and I'd like to use
all classes I have access in the local Runner class but after many
attempts I have failed to do so
I'm using the following code:

 val settings = new Settings()
 settings.embeddedDefaults(classOf[Runner].getClassLoader)

 val i = new IMain(settings) {
   override protected def parentClassLoader: ClassLoader =
classOf[Runner].getClassLoader
 }
 i.setContextClassLoader()
 i.addImports("edu.gemini.aspen.giapi.status.StatusItem")

However the import fails with:
[packageobjects in 0ms]
<console>:7: error: not found: value edu
      import edu.gemini.aspen.giapi.status
             ^

Notice that if I do

println(classOf[Runner].getClassLoader.loadClass("edu.gemini.aspen.giapi.status.StatusItem"))
That works fine

So, it seems IMain is not delegating to the parentClassLoader.
Is there some way to force IMain to use the given classloader?

Regards
Carlos

cquiroz
Joined: 2011-12-09,
User offline. Last seen 42 years 45 weeks ago.
Re: IMain classloader
Hi Aleksey
I tried that as you can see in the code below. Did you also try that in OSGi?
Carlos
On Dec 9, 2011, at 4:02 PM, Aleksey Nikiforov wrote:
I have had this issue recently. The solution that worked for me was:

val imain = new IMain(settings, flusher) {
  override protected def parentClassLoader: ClassLoader = this.getClass.getClassLoader()
}

You can put whatever classloader you want instead of this.getClass.getClassLoader.


On Fri, Dec 9, 2011 at 8:23 AM, cquiroz <carlos.m.quiroz@gmail.com> wrote:
Hi all

I've been trying to integrate a scala interpreter into an application
running in OSGi.
I have a bundle that will crean an IMain instance and I'd like to use
all classes I have access in the local Runner class but after many
attempts I have failed to do so
I'm using the following code:

 val settings = new Settings()
 settings.embeddedDefaults(classOf[Runner].getClassLoader)

 val i = new IMain(settings) {
   override protected def parentClassLoader: ClassLoader =
classOf[Runner].getClassLoader
 }
 i.setContextClassLoader()
 i.addImports("edu.gemini.aspen.giapi.status.StatusItem")

However the import fails with:
[packageobjects in 0ms]
<console>:7: error: not found: value edu
      import edu.gemini.aspen.giapi.status
             ^

Notice that if I do

println(classOf[Runner].getClassLoader.loadClass("edu.gemini.aspen.giapi.status.StatusItem"))
That works fine

So, it seems IMain is not delegating to the parentClassLoader.
Is there some way to force IMain to use the given classloader?

Regards
Carlos


Lex
Joined: 2010-02-28,
User offline. Last seen 42 years 45 weeks ago.
Re: IMain classloader
In order for the interpreter to see classes they must be specified on the interpreter classpath. You must specify the classpath using an instance of Settings that configures the interpreter. Classloader comes in play much later, after the classes have been compiled.


On Fri, Dec 9, 2011 at 1:25 PM, Carlos Quiroz <carlos.m.quiroz@gmail.com> wrote:
Hi Aleksey
I tried that as you can see in the code below. Did you also try that in OSGi?
Carlos

Jon-Erling Dahl
Joined: 2010-11-22,
User offline. Last seen 42 years 45 weeks ago.
Re: IMain classloader

Carlos,

Are you trying to load classes from other bundles? If so a

DynamicImport-Package: *

in your manifest might do the trick.

Cheers,
Jon-Erling

On Dec 9, 2011, at 15:23 , cquiroz wrote:

> Hi all
>
> I've been trying to integrate a scala interpreter into an application
> running in OSGi.
> I have a bundle that will crean an IMain instance and I'd like to use
> all classes I have access in the local Runner class but after many
> attempts I have failed to do so
> I'm using the following code:
>
> val settings = new Settings()
> settings.embeddedDefaults(classOf[Runner].getClassLoader)
>
> val i = new IMain(settings) {
> override protected def parentClassLoader: ClassLoader =
> classOf[Runner].getClassLoader
> }
> i.setContextClassLoader()
> i.addImports("edu.gemini.aspen.giapi.status.StatusItem")
>
> However the import fails with:
> [packageobjects in 0ms]
> :7: error: not found: value edu
> import edu.gemini.aspen.giapi.status
> ^
>
> Notice that if I do
>
> println(classOf[Runner].getClassLoader.loadClass("edu.gemini.aspen.giapi.status.StatusItem"))
> That works fine
>
> So, it seems IMain is not delegating to the parentClassLoader.
> Is there some way to force IMain to use the given classloader?
>
> Regards
> Carlos

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