File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Current Directory for Class File Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Reply Bookmark "Current Directory for Class File" Watch "Current Directory for Class File" New topic
Author

Current Directory for Class File

Neil Johnson
Greenhorn

Joined: Jan 22, 2002
Posts: 11
I would like to be able to open a properties file which is kept in the same directory as the main class file (or jar) that is currently open. However, when I use the System.getProperty() method with various keys, I get back the location of the forte directory, not the location my class file is running from. I have checked the forums and the sun site, with no luck. This should be easy, please help me out.
Thanks, Neil
Zakaria Haque
Ranch Hand

Joined: Jan 02, 2002
Posts: 60
having the properties file in the same directory as the class with main does not help a lot. If you want to access the file only with its name (and not other path info) you need to keep it in the directory from where the JVM was executed.
A better option is to use getResourceAsStream(String) of java.lang.Class or java.lang.ClassLoader , you can do something like
//within main(String[]) of class Foo
Properties prop = new Properties().load((Foo.class).getResourceAsStream("propFileName));
Hope it helps.


tobe bondhu nouka bherao<br />shonabo gaan aj shara raat
Neil Johnson
Greenhorn

Joined: Jan 22, 2002
Posts: 11
Thanks, that worked! The syntax did not work exactly, but the concept worked perfectly. I appreciate you taking the time to answer, and the rest of the stuff in the Class class looks interesting as well. This will make the application a lot easier to use as well.
Thanks, Neil
 
 
subject: Current Directory for Class File
 
Threads others viewed
FileNotFoundException
JSP in Websphere
Servlet Path/Path Info what for ?
File object can't find directory
FileNotFound
MyEclipse, The Clear Choice