Results 1 to 3 of 3

Thread: Resource File

  1. #1
    dvlchd3 is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default 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.

    Java Code:
    configFile.load(this.getClass().getClassLoader().getResourceAsStream("PingConfig.properties"));
    Exception:
    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)
    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?

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    does the this.getClass().getResourceAsStream() work.
    and also, to avoid the exception, separating the line to test if the thing returned is null.
    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");
    }
    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.

  3. #3
    dvlchd3 is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default

    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

  1. The requested resource is not available.
    By ravichandra.t in forum Java Servlet
    Replies: 0
    Last Post: 07-29-2009, 02:18 PM
  2. Jar resource file size
    By OrangeDog in forum Java Applets
    Replies: 1
    Last Post: 04-30-2009, 04:12 AM
  3. Reading a properties file using resource bundle.
    By rudrakanth in forum New To Java
    Replies: 1
    Last Post: 02-27-2009, 12:23 PM
  4. P2P resource sharing
    By vishal.babar07@gmail.com in forum Advanced Java
    Replies: 0
    Last Post: 02-12-2009, 02:48 PM
  5. How to allow one user to acces resource
    By SantoshBK09 in forum Advanced Java
    Replies: 1
    Last Post: 01-06-2009, 03:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •