Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6654371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:22:58+00:00 2026-05-26T01:22:58+00:00

Please consider the following test program (using scala 2.9.0.1) object test { def main(args:Array[String])

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:22:58+00:00Added an answer on May 26, 2026 at 1:22 am

    The second null is explained by java.lang.Class#getClassLoader()

    Returns the class loader for the class. Some implementations may use
    null to represent the bootstrap class loader. This method will return
    null in such implementations if this class was loaded by the bootstrap
    class loader.

    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:

    object Test {
      def main(args:Array[String]) = {
        println(ClassLoader.getSystemClassLoader)
        println(this.getClass.getClassLoader)
        println(classOf[Object].getClassLoader)
      }
    }
    

    and running it with:

    $ scala -cp /temp Test
    

    we get the following output:

    sun.misc.Launcher$AppClassLoader@11b86e7
    URLClassLoader(
      file:/C:/developpement/utils/jdk1.6.0_22/jre/lib/resources.jar
      file:/C:/developpement/utils/jdk1.6.0_22/jre/lib/rt.jar
      file:/C:/developpement/utils/jdk1.6.0_22/jre/lib/jsse.jar
      file:/C:/developpement/utils/jdk1.6.0_22/jre/lib/jce.jar
      file:/C:/developpement/utils/jdk1.6.0_22/jre/lib/charsets.jar
      file:/C:/developpement/utils/jdk1.6.0_22/jre/lib/ext/dnsns.jar
      file:/C:/developpement/utils/jdk1.6.0_22/jre/lib/ext/localedata.jar
      file:/C:/developpement/utils/jdk1.6.0_22/jre/lib/ext/sunjce_provider.jar
      file:/C:/developpement/utils/jdk1.6.0_22/jre/lib/ext/sunmscapi.jar
      file:/C:/developpement/utils/jdk1.6.0_22/jre/lib/ext/sunpkcs11.jar
      file:/C:/DEVELO~1/scala/SCALA-~1.1/bin/../lib/jline.jar
      file:/C:/DEVELO~1/scala/SCALA-~1.1/bin/../lib/scala-compiler.jar
      file:/C:/DEVELO~1/scala/SCALA-~1.1/bin/../lib/scala-dbc.jar
      file:/C:/DEVELO~1/scala/SCALA-~1.1/bin/../lib/scala-library.jar
      file:/C:/DEVELO~1/scala/SCALA-~1.1/bin/../lib/scala-swing.jar
      file:/C:/DEVELO~1/scala/SCALA-~1.1/bin/../lib/scalap.jar
      file:/C:/temp/
    )
    
    null
    

    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)

    java.exe -Xmx256M -Xms32M -Dscala.home="xxx" -cp "libsfromscalahome" scala.tools.nsc.MainGenericRunner  -cp /temp Test
    

    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):

    $ scala -toolcp /temp Test
    

    This option isn’t available in scala.bat. Which means if you’re working under windows, you’ll have to get the resources using

    println(this.getClass.getClassLoader.getResource("toto"))
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider the following fork() / SIGCHLD pseudo-code. // main program excerpt for (;;)
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Please consider the following code: public class Person ( public string FirstName {get; set;}
Please consider the following context from Innate : # Default application for Innate def
consider the following example: public IEnumerable<String> Test () { IEnumerable<String> lexicalStrings = new List<String>
Please consider the following table (created using a corresponding entity) request ------- id requestor
please consider the following: I have a queue of objects represented as an array.
Consider the following two Java classes: a.) class Test { void foo(Object foobar) {
Please consider the following: public class MyObject { public bool B; public string Txt;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.