[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

can't find properties in jar file

run:
java -classpath "lib/myproject.jar" com.mycompany.MyApp

MyApp code:
Properties pr = new Properties();
pr.load(new FileInputStream("my.properties"));

my.properties sits in the same directory as the exec:

com\mycompany\MyApp.class
com\mycompany\my.properties

whenever I execute the class I get "FileNotFoundException" on the properties file. How do I get the class to locate it?



0
deal051298
Asked:
deal051298
  • 5
  • 5
1 Solution
 
objectsCommented:
If your resource is located inside the jar file then you will need to instead read it using the class loader.
Something like:

pr.load(ClassLoader.getSystemResourceAsStream("com/mycompany/my/properties"));

Or from MyApp try:

pr.load(getClass().getResourceAsStream("my.properties"));
0
 
deal051298Author Commented:
I have to try the first one since it's in a static method. Still doesn't work - I am getting null pointer error as getSystemResourceAsStream() returns null regardless of the path I use:

ClassLoader.getSystemResourceAsStream("/com/mycompany/myapp/my/properties")
ClassLoader.getSystemResourceAsStream("com/mycompany/myapp/my/properties")
ClassLoader.getSystemResourceAsStream("./budgetmonitor/properties")
ClassLoader.getSystemResourceAsStream("/com/mycompany/myapp/my.properties")
ClassLoader.getSystemResourceAsStream("com/mycompany/myapp/my.properties")
ClassLoader.getSystemResourceAsStream("./my.properties")

Cheers
0
 
deal051298Author Commented:
sorry 3rd one should be:

ClassLoader.getSystemResourceAsStream("./my/properties")
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
objectsCommented:
Why can't you use the second one?
0
 
deal051298Author Commented:
when I try it it just returns null
0
 
objectsCommented:
Are you calling getResourceAsStream from your MyApp class?
ANd is the properties file in your jar in the same directory as MyApp.class?
0
 
deal051298Author Commented:
Yes and Yes (double/triple/quadruple checked spelling and jar).

I notice that my ant compile create 2 class file for this class.

I have one src file:

com/mycompany/myapp/MyApp.class

and it compiles to:

com/mycompany/myapp/MyApp$1.class and
com/mycompany/myapp/MyApp.class

and the $1 class is 1/20th he size of the main one. Would this have anything to do with it?

Thanks heaps
0
 
objectsCommented:
No they're just inner classes.

From what you've said in your question the correct calls should be any of:

ClassLoader.getSystemResourceAsStream("com/mycompany/my.properties")

OR

MyApp.class.getResourceAsStream("my.properties"));

OR even:

getClass().getResourceAsStream("com/mycompany/my.properties"));


However in your comments you have included a myapp in the package name that is not mentioned in the question so it's a bit unclear where thing actually are.
0
 
deal051298Author Commented:
sorry for the confusion :-(

locations of files are:

com/mycompany/myapp/my.properties
com/mycompany/myapp/MyApp.class

Didn't see your 2nd solution so tried that - and it works!

pr.load(MyApp.class.getResourceAsStream("my.properties"));

Cheers!
0
 
objectsCommented:
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now