Solved

can't find properties in jar file

Posted on 2002-05-06
10
243 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
  • 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now