Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
scabug opened this issue Oct 12, 2011 · 3 comments
Assignees
Labels
Milestone

Comments

@scabug
Copy link

scabug commented Oct 12, 2011

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.

@scabug
Copy link
Author

scabug commented Oct 12, 2011

Imported From: https://issues.scala-lang.org/browse/SI-5072?orig=1
Reporter: ryanobjc

@scabug
Copy link
Author

scabug commented Jan 20, 2012

@retronym said (edited on Jan 20, 2012 10:04:21 PM UTC):
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

@scabug
Copy link
Author

scabug commented Jan 21, 2012

@retronym said:
scala/scala#121

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants