Results 1 to 7 of 7
- 02-03-2011, 06:23 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 85
- Rep Power
- 0
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?
- 02-03-2011, 06:59 PM #2
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!
- 02-03-2011, 07:04 PM #3
Member
- Join Date
- Sep 2008
- Posts
- 85
- Rep Power
- 0
Here is my code. Let me know if I need to do anything different
and I also tried the code given below. Both did not workJava 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); } }
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.
- 02-03-2011, 07:20 PM #4
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!
- 02-03-2011, 07:26 PM #5
Member
- Join Date
- Sep 2008
- Posts
- 85
- Rep Power
- 0
Thanks Kevin, I will try this tomorrow and let you know!
- 02-03-2011, 07:33 PM #6
Member
- Join Date
- Sep 2008
- Posts
- 85
- Rep Power
- 0
kevin: do i also need to mention this properties file in the manifest file?
- 02-04-2011, 05:46 AM #7
Member
- Join Date
- Sep 2008
- Posts
- 85
- Rep Power
- 0
Hi, I tried with
and also withJava Code:inStream = this.getClass().getClassLoader().getResourceAsStream("/mailer.properties");
but in both the cases I gotJava Code:inStream = this.getClass().getClassLoader().getResourceAsStream("mailer.properties");
Exception in thread "main" java.lang.NullPointerException
at com.abc.gip.mailer.utils.MailerProperties.<init>(M ailerPropertie
s.java:31)
Similar Threads
-
Having trouble reading external txt file to an array
By Metastar in forum New To JavaReplies: 18Last Post: 07-21-2010, 11:29 PM -
Including external file in JSP
By ulix83 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 05-10-2010, 10:31 AM -
Reading and Writing the contents of a file to another file
By priyankatxs in forum New To JavaReplies: 9Last Post: 10-20-2009, 10:52 AM -
[SOLVED] how to reading binary file and writing txt file
By tOpach in forum New To JavaReplies: 3Last Post: 05-09-2009, 11:31 PM -
Use a external file in my program
By romina in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks