Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Current Directory for Class File  RSS feed

 
Neil Johnson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Neil Johnson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!