Opening a file that is on the classpath.

K

Kevin

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
 
D

Daniel Pitts

Kevin said:
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.
 
L

Luke Yan

ÔÚ Sat, 06 Oct 2007 10:16:57 -0700 ʱ, Daniel Pitts
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...
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
 
L

Lew

Luke said:
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.

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.

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".

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top