« Return to Thread: [scala] problem with getResource

Re: [scala] problem with getResource

by Christos KK Loverdos :: Rate this Message:

Reply to Author | View in Thread

Some parts of this message have been removed. Learn more about Nabble's security policy.
Hi Gregory,

Generally speaking, do not rely on classloaders to follow your path in the disk. Now, to your case, I cannot reproduce the behaviour (I have made the folder and files as in your case):

C:\>java-apps\scala-2.7.1-final\bin\scala -Xnojline
Welcome to Scala version 2.7.1.final (Java HotSpot(TM) Client VM, Java 1.6.0_07).

Type in expressions to have them evaluated.
Type :help for more information.

scala> new java.io.File("WorkspaceDecls")
res0: java.io.File = WorkspaceDecls

scala> res0.listFiles()
res1: Array[java.io.File] = Array(WorkspaceDecls\App.class, WorkspaceDecls\gemcutter.default.cws)

scala> java.lang.Thread.currentThread().getContextClassLoader()
res2: java.lang.ClassLoader = java.net.URLClassLoader@354093

scala> res2.getResource("WorkspaceDecls/gemcutter.default.cws")
res3: java.net.URL = file:/C://WorkspaceDecls/gemcutter.default.cws


Since in Scala 2.7.1 the context class loader is a URLClassLoader, please do the following, just to see if the folder containing WorkspaceDecls is the classpath of the classloader:


scala> res2.asInstanceOf[java.net.URLClassLoader].getURLs

If you don't see it here, then you get normal behaviour. Otherwise, we shall have to dig a little further :-)

BR
Christos

On 7/23/08, Meredith Gregory <lgreg.meredith@...> wrote:
Scalads and Lasses,

Please see the trace below. The method getResource on java.lang.Thread.currentThread().getContextClassLoader() is not behaving as i expect it would. It never returns anything but null as far as my experimentation has gone. Any help would be greatly appreciated.

Best wishes,

--greg

[INFO] [scala:console]
Welcome to Scala version 2.7.1.final (Java HotSpot(TM) Client VM, Java 1.5.0_13).
Type in expressions to have them evaluated.
Type :help for more information.

scala> new java.io.File("WorkspaceDecls")
new java.io.File("WorkspaceDecls")
new java.io.File("WorkspaceDecls")
res0: java.io.File = WorkspaceDecls

scala> res0.listFiles()
res0.listFiles()
res0.listFiles()
res1: Array[java.io.File] = Array(WorkspaceDecls/App.class, WorkspaceDecls/gemcutter.default.cws)

scala> java.lang.Thread.currentThread().getContextClassLoader()
java.lang.Thread.currentThread().getContextClassLoader()
java.lang.Thread.currentThread().getContextClassLoader()
res2: java.lang.ClassLoader = java.net.URLClassLoader@697c4

scala> res2.getResource("WorkspaceDecls/gemcutter.default.cws")
res2.getResource("WorkspaceDecls/gemcutter.default.cws")
res2.getResource("WorkspaceDecls/gemcutter.default.cws")
res3: java.net.URL = null

scala> new java.io.File(".").getCanonicalPath()
new java.io.File(".").getCanonicalPath()
new java.io.File(".").getCanonicalPath()
res4: java.lang.String = /Users/lgm/work/src/projex/bobj/magritte/pipe-cutter/pipes_to_cal

scala> res2.getResource(new java.io.File(".").getCanonicalPath() + "WorkspaceDecls/gemcutter.default.cws")
res2.getResource(new java.io.File(".").getCanonicalPath() + "WorkspaceDecls/gemcutter.default.cws")
res2.getResource(new java.io.File(".").getCanonicalPath() + "WorkspaceDecls/gemcutter.default.cws")
res5: java.net.URL = null

scala>

--
L.G. Meredith
Managing Partner
Biosimilarity LLC
806 55th St NE
Seattle, WA 98105

+1 206.650.3740

http://biosimilarity.blogspot.com



--
  __~O
-\ <,       Christos KK Loverdos
(*)/ (*)      http://ckkloverdos.com

 « Return to Thread: [scala] problem with getResource

LightInTheBox - Buy quality products at wholesale price!