« Return to Thread: [scala] problem with getResource
You could try a shotgun approach
public class X {
public static InputStream getResourceAsStream( String resourceName )
{
InputStream result = null;
// try the current threads classloader
result = Thread.currentThread().getContextClassLoader().getSystemResourceAsStream( "/" + resourceName );
if ( result == null )
{
result = Thread.currentThread().getContextClassLoader().getSystemResourceAsStream( resourceName );
}
// try this class's classloader
if ( result == null )
{
result = X.class.getResourceAsStream( "/" + resourceName );
}
// try this class's classloader
if ( result == null )
{
result = X.class.getResourceAsStream( resourceName );
}
// try system class loader
if ( result == null )
{
result = ClassLoader.getSystemResourceAsStream( "/" + resourceName );
}
// try system class loader
if ( result == null )
{
result = ClassLoader.getSystemResourceAsStream( resourceName );
}
return result;
}
}On Thu, Jul 24, 2008 at 3:10 AM, Meredith Gregory <lgreg.meredith@...> wrote:Christos,
Many thanks for your reply. In my experimentation i did find that the ClassLoader returned from getContextClassLoader is indeed a URLClassLoader. i note that there is a big difference between your system and mine: your box is running some version of Windows, while my MacBook Pro is running the latest update of Leopard.
As for relying on classloaders... unfortunately the code is not mine to make that decision. i'm integrating some Scala functionality with CAL functionality. The Quark/CAL initialization is failing here because the getResource method is returning null under every invocation i can think of. Since i have the CAL source i looked through it relatively carefully. i believe they wrote it this way by way of following their interpretation of best practices. Moreover, i tried putting in a small hack of the form replacing
if (folderFile == null) {
return null;
}
withif (folderFile == null) {
try {
File lastChanceFile = new File(new File(".").getCanonicalPath() + filePath.getPathString());
if (lastChanceFile.exists()) {
return lastChanceFile;
}
} catch (java.io.IOException e) {
return null;
}
return null;
}
But even though this actually returns the file, all hell breaks loose further on. So, i think what i have to do is attempt invoke the Scala code that calls the CAL/Quark API with the exact same environment that they use in the ICE.sh invocation script. My guess is that they never got a chance to test their APIs in a inter-JVM-language situation.
Best wishes,
--gregOn Wed, Jul 23, 2008 at 12:45 AM, Christos KK Loverdos <loverdos@...> wrote:
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 -XnojlineWelcome 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)res2: java.lang.ClassLoader = java.net.URLClassLoader@354093
scala> java.lang.Thread.currentThread().getContextClassLoader()
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
ChristosOn 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
--
L.G. Meredith
Managing Partner
Biosimilarity LLC
806 55th St NE
Seattle, WA 98105
+1 206.650.3740
http://biosimilarity.blogspot.com
« Return to Thread: [scala] problem with getResource
| Free Forum Powered by Nabble | Forum Help |