Please consider the following test program (using scala 2.9.0.1)
object test
{
def main(args:Array[String]) = {
println(ClassLoader.getSystemClassLoader.getResource("toto"))
println(this.getClass.getClassLoader.getResource("toto"))
println(classOf[Object].getClassLoader)
}
}
I compile it and run it with "-cp /tmp" containing a file "toto", and I get the following output:
null
file:/tmp/toto
null
=> the system classloader does not contain the classpath
=> the Object class has no classloader!
Am I missing something there or is it a (big) bug in scala?!
Thanks, Arjun
The second null is explained by java.lang.Class#getClassLoader()
So, this is why
classOf[Object].getClassLoader
returns null, it's loaded by the bootstrap classloader (it is in rt.jar, more specifically, it is in a jar which is in $JAVA_HOME/lib).The first null is harder to explain. It seems that Scala leaves the system classloader as-is, and only adds the options -cp to it's own classloader (ScalaClassLoader in scala/util/ClassLoader.scala).
Using the following:
and running it with:
we get the following output:
So the System classloader is left untouched, but the Scala classloader gets the items from -cp added to it.
Moral of the story: don't use the system classloader in Scala if you want to access resources from the classpath.
EDIT: Ok, I've investigated this a bit more, and scala.bat is executing the following command line (under pure Windows, shortened for readability)
So the -cp option from the command line is only being passed as an option to MainGenericRunner, not the java. I believe, from looking at the code, that under unix you can specify the -toolcp option to scala to get something included in the java classpath. Something like (totally untested):
This option isn't available in scala.bat. Which means if you're working under windows, you'll have to get the resources using
I couldn't find an issue in the Scala Lang Issues, but if it's a problem for you, raise an issue and submit a fix. I'm sure they'll be thrilled :-)
EDIT: I have raised this as issue SI 5062 -toolcp should be available on windows, in the scala.bat, and provided a pull request for it on github.
From Wikipedia's classloader article:
Not sure about the second null though. Maybe someone else can clear that up.