Results 1 to 7 of 7
  1. #1
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default Reading file external to jar file

    I have a jar file which needs to read a properties file in the same location as the jar file. I get a filenotfound exception when I run the following command
    D:\emailer>java -jar test.jar

    the test.jar and the .properties file are in the same folder i.e d:\emailer

    however when i run this app from eclipse its able to find the prop file and no errors thrown. Do I need to do anything different when i run it as a jar file?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default

    What code do you use to access the file? Post it in the form of an SSCCE, and don't forget the code tags.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default

    Here is my code. Let me know if I need to do anything different
    Java Code:
    public class MailerProperties {
    
    	private static Properties properties;
    	
    	public MailerProperties() {
    		properties = new Properties();
    		InputStream inStream;
    		try {
    			inStream = (new ClassPathResource("mailer.properties")).getInputStream();			
    			properties.load(inStream);
    		} catch (IOException e) {			
    			e.printStackTrace();
    		}	
    	}
    	
    	public String getValue(String key) {
    		return properties.getProperty(key);
    	}
    
    	
    }
    and I also tried the code given below. Both did not work

    Java Code:
    File f = new File("mailer.properties");
    if(f.exists()){
        Properties pro = new Properties();
        FileInputStream in = new FileInputStream(f);
        pro.load(in);
        // rest of code that handle the values from file....
    }
    Last edited by nn12; 02-03-2011 at 07:08 PM.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default

    That's not an SSCCE because it's not runnable and it contains a bunch of classes not from the standard API.

    But I'll just throw this out there as a hint:

    Java Code:
    InputStream fileNextToJarStream = this.getClass().getClassLoader().getResourceAsStream("FileNextToJar.txt");
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default

    Thanks Kevin, I will try this tomorrow and let you know!

  6. #6
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default

    kevin: do i also need to mention this properties file in the manifest file?

  7. #7
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default

    Hi, I tried with
    Java Code:
    inStream = this.getClass().getClassLoader().getResourceAsStream("/mailer.properties");
    and also with
    Java Code:
    inStream = this.getClass().getClassLoader().getResourceAsStream("mailer.properties");
    but in both the cases I got
    Exception in thread "main" java.lang.NullPointerException
    at com.abc.gip.mailer.utils.MailerProperties.<init>(M ailerPropertie
    s.java:31)

Similar Threads

  1. Having trouble reading external txt file to an array
    By Metastar in forum New To Java
    Replies: 18
    Last Post: 07-21-2010, 11:29 PM
  2. Including external file in JSP
    By ulix83 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-10-2010, 10:31 AM
  3. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  4. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  5. Use a external file in my program
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:28 AM

Posting Permissions

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