[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

can't find properties in jar file

Posted on 2002-05-06
10
Medium Priority
?
252 Views
Last Modified: 2013-11-23
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
Comment
Question by:deal051298
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6993075
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
 

Author Comment

by:deal051298
ID: 6995552
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
 

Author Comment

by:deal051298
ID: 6995555
sorry 3rd one should be:

ClassLoader.getSystemResourceAsStream("./my/properties")
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 6995566
Why can't you use the second one?
0
 

Author Comment

by:deal051298
ID: 6995568
when I try it it just returns null
0
 
LVL 92

Expert Comment

by:objects
ID: 6995580
Are you calling getResourceAsStream from your MyApp class?
ANd is the properties file in your jar in the same directory as MyApp.class?
0
 

Author Comment

by:deal051298
ID: 6995585
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
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 6995597
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
 

Author Comment

by:deal051298
ID: 6995675
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
 
LVL 92

Expert Comment

by:objects
ID: 6995703
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question