| Author |
Reading a file which is specified in CLASSPATH
|
Pranav Pal
Ranch Hand
Joined: Nov 04, 2007
Posts: 74
|
|
We generally give the filepath like: File file = new File(filepath); But now if my filepath is stored in CLASSPATH environment variable, then how can I access this filepath in my program using CLASSPATH variable or by some another mechanism and not specifying the absolute or relative path to the file?
|
Hakuna Matata!
|
 |
Jan Cumps
Bartender
Joined: Dec 20, 2006
Posts: 2343
|
|
|
Class.getResourceAsStream() allows you to read files in the classpath.
|
OCUP UML fundamental
ITIL foundation
|
 |
Rajesh Narkhed
Greenhorn
Joined: Jan 03, 2008
Posts: 6
|
|
You will need to read the CLASSPATH env variable as String claspathValue = System.getProperty("java.class.path")); and then parse the value of claspathValue String to fetch the location of file that you want.
|
 |
Jan Cumps
Bartender
Joined: Dec 20, 2006
Posts: 2343
|
|
Originally posted by Rajesh Narkhed: You will need to read the CLASSPATH env variable... and then parse the value of claspathValue String to fetch the location of file that you want.
How would that work? A classpath contains several entries, pointing either to jars or to directories. How can you parse and fetch a file location from that? Regards, Jan
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
But you need the reference to be relative to some class, or absolute for some class loader. How do you know which class?
Originally posted by Jan Cumps: How would that work? A classpath contains several entries, pointing either to jars or to directories. How can you parse and fetch a file location from that? Regards, Jan
Split on File.separator*, convert each element to a File object and check that. * This works in all cases except if a file contains File.separator. Which it cannot on most OSes.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Jan Cumps
Bartender
Joined: Dec 20, 2006
Posts: 2343
|
|
But you need the reference to be relative to some class, or absolute for some class loader. How do you know which class?
As long as the file is in a directory that is listed in the classpath, you can load it by simply using the filename as parameter. Regards, Jan
|
 |
 |
|
|
subject: Reading a file which is specified in CLASSPATH
|
|
|