Scala Programming Language
  1. Scala Programming Language
  2. SI-5072

Thread.currentThread.getContextClassLoader isnt set up properly in the console/repl

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: Scala 2.10.0
    • Component/s: Repl / Interpreter
    • Labels:
      None

      Description

      Thread.currentThread.getContextClassLoader returns a stub of a classloader:

      scala> Thread.currentThread.getContextClassLoader
      res15: java.lang.ClassLoader = sun.misc.Launcher$AppClassLoader@53004901

      This resulting classloader is unable to load resources from the system classpath, so any code you call from within the console may fail. For example, the Hadoop project uses the context class loader to obtain configuration files from the jars. But it will fail to find config entries (bad):

      scala> val c = new Configuration
      c: org.apache.hadoop.conf.Configuration = Configuration: core-default.xml, core-site.xml

      scala> c.size
      res12: Int = 0

      One cannot set the Thread.currentThread.setContextClassLoader because every new line is a new thread. Thus perhaps one could fix it by doing a block first setting the context class loader, then calling the code which needs it like such.

      Essentially the result is legitimate code which will run in a script/app in scala will not run in a console.

        Issue Links

          Activity

          Hide
          Jason Zaugg added a comment - - edited

          scala -Dscala.repl.no-threads=true or scala -Yrepl-sync avoids spawning new threads for each line in Scala 2.9.1

          In Scala 2.10.0-M1, this is classloader covers classes created during the REPL session.

          ~/code/scala scala210 -Yrepl-sync
          Welcome to Scala version 2.10.0-M1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29).
          Type in expressions to have them evaluated.
          Type :help for more information.
          
          scala> class C
          defined class C
          
          scala> Thread.currentThread.getContextClassLoader.loadClass(classOf[C].getName)
          res0: Class[_] = class C
          
          Show
          Jason Zaugg added a comment - - edited scala -Dscala.repl.no-threads=true or scala -Yrepl-sync avoids spawning new threads for each line in Scala 2.9.1 In Scala 2.10.0-M1, this is classloader covers classes created during the REPL session. ~/code/scala scala210 -Yrepl-sync Welcome to Scala version 2.10.0-M1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29). Type in expressions to have them evaluated. Type :help for more information. scala> class C defined class C scala> Thread.currentThread.getContextClassLoader.loadClass(classOf[C].getName) res0: Class[_] = class C
          Show
          Jason Zaugg added a comment - https://github.com/scala/scala/pull/121

            People

            • Assignee:
              Jason Zaugg
              Reporter:
              ryanobjc
            • Votes:
              2 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development