Results 1 to 3 of 3
Thread: Resource File
- 01-12-2010, 09:50 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 2
- Rep Power
- 0
Resource File
Hello. I'm sure this is most likely a duplicate entry, however, I've been searching for about an hour now, and everything I've tried has given me no success.
I have a resource file that will be delivered with an executable JAR file (not within the JAR file). My question is, where should I put this file in Eclipse, and what path should I use to access this file. Keep in mind, this application will be run on multiple operating systems, therefore, using the absolute path to the file works now, however, will not work implementation time.
On a different but related note, I can't even get this darn file to load in Eclipse currently. The file is in directory with the package that is attempting to access it (both in src and bin directories). However, the below code throws an exception.
Exception:Java Code:configFile.load(this.getClass().getClassLoader().getResourceAsStream("PingConfig.properties"));
What am I doing wrong here, first of all, and secondly, is there an appropiate way to configure the project so when I export the project as a JAR, the config file will be able to be read from outside the JAR file?Java Code:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Unknown Source) at java.util.Properties.load0(Unknown Source) at java.util.Properties.load(Unknown Source) at my.package.name.is.here.<init>(PingConstants.java:52) at GUI.<init>(GUI.java:46) at GUI$4.run(GUI.java:358) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
- 01-13-2010, 03:39 AM #2
does the this.getClass().getResourceAsStream() work.
and also, to avoid the exception, separating the line to test if the thing returned is null.
another idea, if PingConfig.properties is in the top level package, the path might need "/" in front of it, otherwise, the get resource as stream might try to look for this file in the current java package as where ever this class is in. or possibly the package the class loader is in.Java Code:InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("PingConfig.properties"); if (inputStream != null) { configFile.load(inputStream); } else { // do what ever error message to indicate can't find the file here System.error.println("oops, can't find the file PingConfig.properties"); }
- 01-13-2010, 10:32 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 2
- Rep Power
- 0
Thank you for your response. You're correct, the getResourceAsStream() method does not seem to work. I, instead, opened a File, verified it existance and readability, and then sent a FileInputStream into the load method, and everything worked. Not really sure why the other way didn't work, but at least I can load the file now. Thanks for your help!
Similar Threads
-
The requested resource is not available.
By ravichandra.t in forum Java ServletReplies: 0Last Post: 07-29-2009, 02:18 PM -
Jar resource file size
By OrangeDog in forum Java AppletsReplies: 1Last Post: 04-30-2009, 04:12 AM -
Reading a properties file using resource bundle.
By rudrakanth in forum New To JavaReplies: 1Last Post: 02-27-2009, 12:23 PM -
P2P resource sharing
By vishal.babar07@gmail.com in forum Advanced JavaReplies: 0Last Post: 02-12-2009, 02:48 PM -
How to allow one user to acces resource
By SantoshBK09 in forum Advanced JavaReplies: 1Last Post: 01-06-2009, 03:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks