Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

Opening a file that is on the classpath.

Thread view: 
Kevin - 05 Oct 2007 18:25 GMT
Hi

Is there any easy way to read a file off of the classpath?  Say I have
file X that is in the root of a jar I include in my classpath.  Is
there something like a new FileInputStream("classpath:/X") ?

Thanks,
Kevin
Stefan Ram - 05 Oct 2007 18:32 GMT
>Is there any easy way to read a file off of the classpath?

http://download.java.net/jdk7/docs/api/java/lang/ClassLoader.html#getSystemResou
rce(java.lang.String
)
http://download.java.net/jdk7/docs/api/java/net/URL.html#openStream()
Kevin - 05 Oct 2007 19:39 GMT
Greatly appreciated Stefan.  Exactly what I was looking for.

Regards,
Kevin
Daniel Pitts - 06 Oct 2007 18:16 GMT
> Greatly appreciated Stefan.  Exactly what I was looking for.
>
> Regards,
> Kevin

There is also Class.getResource, which tries to find the resource (I
think in the same package). and ClassLoader.getResource(). I'm not sure
how getResource differs from getSystemResource(), I always use
getResource(), so you might want to verify which one fits your situation
the best...

HTH,
Daniel.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Luke Yan - 07 Oct 2007 00:41 GMT
ÔÚ Sat, 06 Oct 2007 10:16:57 -0700 ʱ, Daniel Pitts
<newsgroup.spamfilter@virtualinfinity.net> дÁË:
--

>> Greatly appreciated Stefan.  Exactly what I was looking for.
>>
[quoted text clipped - 6 lines]
>getResource(), so you might want to verify which one fits your situation
>the best...

in my option, java has two kinds  of library, system library and none-system
library (i am not sure whether the names are correct). System library is located
in JRE or JDK directory and none-system library is added to CLASSPATH by user.
So, "getSystemResource()" will find the resource in system library

>HTH,
>Daniel.

--
MSN: gumpagain44#hotmail.com
/**
/* Java Is Not Platform-independent.It Is The Platform!
*/
Lew - 07 Oct 2007 02:22 GMT
> in my option, java has two kinds  of library, system library and none-system
> library (i am not sure whether the names are correct). System library is located
> in JRE or JDK directory and none-system library is added to CLASSPATH by user.
> So, "getSystemResource()" will find the resource in system library

You guys got me curious, so I looked it up.

<http://java.sun.com/javase/6/docs/api/java/lang/ClassLoader.html#getResource(jav
a.lang.String
)>
> public URL getResource(String name)
> This method will first search the parent class loader for the resource;
> if the parent is null the path of the class loader built-in to the
> virtual machine is searched. That failing, this method will invoke
> findResource(String) to find the resource.

<http://java.sun.com/javase/6/docs/api/java/lang/ClassLoader.html#getSystemResour
ce(java.lang.String
)>
> public static URL getSystemResource(String name)
> Find a resource of the specified name from the search path used to load classes.
> This method locates the resource through the system class loader
> (see getSystemClassLoader())

So, the static getSystemResource() is like the instance method getResource()
if it were called through the system Classloader instance.

Nothing yet about "system library" vs. "non-system library".

<http://java.sun.com/javase/6/docs/api/java/lang/ClassLoader.html#getSystemClassLoader()>
> public static ClassLoader getSystemClassLoader()
> Returns the system class loader for delegation.
> This is the default delegation parent for new ClassLoader instances,
> and is typically the class loader used to start the application.
...
> The default system class loader is an implementation-dependent instance  of [ClassLoader].

Still nothing yet about "system library" vs. "non-system library".

There isn't such a distinction.  The system class loader is often the only
class loader in an application, and loads everything from every class path.
It's only when the application instantiates its own ClassLoaders that the
methods diverge.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.