?
Solved

can't find properties in jar file

Posted on 2002-05-06
10
Medium Priority
?
251 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month12 days, 21 hours left to enroll

777 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