Professional Documents
Culture Documents
Developers Home > Developer Forums > Core > Core APIs > Reflections & Reference Objects >
Developer Forums
Reflections & Reference Objects -
ClassLoader.getResourceAsStream vs.
Class.getResourceAsStream
Sun Forums
Back to Forum Feedback About Forums
Welcome
5 Duke Stars available This topic has 1 reply on 1 page.
» Login
» Watch List
» Duke Stars Program E-mail this Topic Watch this topic
» My Forums
» Feedback
» FAQ medale94 ClassLoader.getResourceAsStream vs.
» Code of Conduct Posts:4 Class.getResourceAsStream
Registered: May 3, 2002 10:52 AM Reply »
10/29/99
Search Forums Does anyone know why the two methods are implemented with
different semantics? It seems that Class.getResourceAsStream
» supports the more intuitive absolute vs. relative path concept, while
ClassLoader.getResourceAsStream only supports absolute path
with "/" implied:
Using:
this.getClass().getResourceAsStream("hello.txt")
prints out:
File in /medale/test/hello.txt - File read in relative to current class
which was loaded from /medale/test
Using:
this.getClass().getResourceAsStream("/hello.txt")
prints out:
File in /hello.txt - File read using absolute path
Using:
this.getClass().getResourceAsStream("medale/test/hello.txt")
prints out:
1 of 5 2/22/2008 12:27 PM
Reflections & Reference Objects - ClassLoader.getResourceAsStream vs... http://forum.java.sun.com/thread.jspa?threadID=251367&messageID=...
Using:
this.getClass().getResourceAsStream("/medale/test/hello.txt")
Prints out:
File in /hello.txt
Using:
this.getClass().getClassLoader().getResourceAsStream("/hello.txt")
Returns null
package medale.test;
import java.io.*;
2 of 5 2/22/2008 12:27 PM
Reflections & Reference Objects - ClassLoader.getResourceAsStream vs... http://forum.java.sun.com/thread.jspa?threadID=251367&messageID=...
(this.getClass().
getClassLoader().
getResourceAsStream(RES_FILES)));
if (bin != null) {
System.out.println(bin.readLine());
bin.close();
} else {
System.out.println("Unable to retrieve InputStream");
}
} catch (Exception e) {
System.out.println(e);
}
}
System.out.println("\n\nRunning code using
Thread.currentThread()."
+ "getContextClassLoader()."
+ "getResourceAsStream(RES_FILES)");
for (int i=0; i < RES_FILES.length; i++) {
try {
System.out.print("Result generated using file path >>"
+ RES_FILES
+"<< : ");
BufferedReader bin = new BufferedReader
(new InputStreamReader
(Thread.currentThread().
getContextClassLoader().
getResourceAsStream(RES_FILES)));
if (bin != null) {
System.out.println(bin.readLine());
bin.close();
} else {
System.out.println("Unable to retrieve InputStream");
}
} catch (Exception e) {
System.out.println(e);
}
}
}
3 of 5 2/22/2008 12:27 PM
Reflections & Reference Objects - ClassLoader.getResourceAsStream vs... http://forum.java.sun.com/thread.jspa?threadID=251367&messageID=...
</target>
<!-- Compile all source code in src and place in classes -->
<target name="compile" depends="prepare">
<javac classpath="${class.dir}"
srcdir="${src.dir}"
destdir="${class.dir}"/>
</target>
<target name="clean">
<delete dir="${class.dir}"/>
</target>
</project>
Thanks,
Markus
The system ClassLoader will search for the given file relative to the
system classpath... although I am surprised that including a leading
slash means that /classes/hello.txt isn't found.
4 of 5 2/22/2008 12:27 PM
Reflections & Reference Objects - ClassLoader.getResourceAsStream vs... http://forum.java.sun.com/thread.jspa?threadID=251367&messageID=...
About Sun | About This Site | Newsletters | Contact Us | A Sun Developer Network
Employment Site
How to Buy | Licensing | Terms of Use | Privacy | Trademarks
Unless otherwise licensed, code
in all technical manuals herein
Copyright 1994-2008 Sun Microsystems, Inc. (including articles, FAQs,
samples) is provided under this
License.
Content Feeds
5 of 5 2/22/2008 12:27 PM