lihaoyi test$ tree
.
└── Foo.scala
0 directories, 1 file
lihaoyi test$ cat Foo.scala
object Main{
def main(args: Array[String]): Unit = {
println(getClass.getClassLoader.getResourceAsStream("java/lang/String.class"))
println(getClass.getClassLoader.getClass)
println(Thread.currentThread().getContextClassLoader.getResourceAsStream("java/lang/String.class"))
println(Thread.currentThread().getContextClassLoader.getClass)
}
}
lihaoyi test$ sbt run
[info] Loading global plugins from /Users/lihaoyi/.sbt/0.13/plugins
[info] Set current project to test (in build file:/Users/lihaoyi/Dropbox/Workspace/test/)
[info] Updating {file:/Users/lihaoyi/Dropbox/Workspace/test/}test...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/lihaoyi/Dropbox/Workspace/test/target/scala-2.10/classes...
[info] Running Main
[email protected]8ff2
class sbt.classpath.ClasspathUtilities$$anon$1
null
class sbt.classpath.ClasspathFilter
[success] Total time: 2 s, completed 29 May, 2017 4:14:11 PM
lihaoyi test$
यहां, हम देख सकते हैं कि getClass.getClassLoader
और Thread.currentThread.getContextClassLoader
विभिन्न मान लौटा रहे हैं। और क्या है, Thread.currentThread.getContextClassLoader
java/lang/String.class
लोड करने से इंकार कर रहा है, जबकि दूसरा कर सकता है।क्यों एसबीटी के थ्रेड संदर्भ क्लासलोडर संसाधनों के रूप में जेडीके क्लासफाइल्स लोड नहीं कर सकते?
विशेष रूप से, जब मैं scalac
/scala
, या java
की तरह एक बाहरी उपकरण का उपयोग कर जार फ़ाइल को चलाने, दोनों classloaders एक संसाधन
lihaoyi test$ scalac Foo.scala
lihaoyi test$ scala Main
[email protected]cdfa
class scala.reflect.internal.util.ScalaClassLoader$URLClassLoader
[email protected]724f
class scala.reflect.internal.util.ScalaClassLoader$URLClassLoader
मैं एसबीटी समान व्यवहार की उम्मीद करेंगे के रूप में classfile लोड करने में सक्षम हैं: Main.getClass.getClassLoader
और Thread.currentThread().getContextClassLoader
दोनों संसाधन के रूप में java/lang/String.class
लोड करने में सक्षम होंगे। क्या देता है?