aspose file tools*
The moose likes Beginning Java and the fly likes Reading a file which is specified in CLASSPATH Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading a file which is specified in CLASSPATH" Watch "Reading a file which is specified in CLASSPATH" New topic
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: 2516
    
  10

Class.getResourceAsStream() allows you to read files in the classpath.


OCUP UML fundamental and ITIL foundation
youtube channel
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: 2516
    
  10

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: 19787
    
  20

Originally posted by Jan Cumps:
Class.getResourceAsStream() allows you to read files in the classpath.

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2516
    
  10

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading a file which is specified in CLASSPATH