Solved

can't find properties in jar file

Posted on 2002-05-06
10
248 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 125 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

738 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