?
Solved

Shall I include my properties file in Jar ?

Posted on 2003-03-05
31
Medium Priority
?
437 Views
Last Modified: 2013-11-23
I have a small application with couples of classes and a properties file where I keep my configuration items. I use

props = new Properties();
props.load(this.getClass().getResourceAsStream("myProps.properties"));

to load my properties and therefore I keep my properties file with my class files.
Now I need to distribute this application and the easiest way is to create a jar file. Now when I include my properties file in jar file, I just have one jar file which I can send to my users and it works very well but the problem is, if user want to change configuration values, which is in the properties file, then there is no easy way to do this as they have to extract, edit and re add properties file in the jar; or in other word they have to coome to me.

My question is, Do we usually package properties file with jar ? If not how can I keep my properties file out side jar and still class path load it ? Or is there is any other way to easily distribute my application with or without jar file where they can easily run the application and also have flexibility to change properties file.
My application does not involve any GUI.
Thanks in advance.
0
Comment
Question by:makunag
[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
  • 14
  • 11
  • 5
  • +1
31 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8071378
Why not write a small application to edit this file and put it in the same jar?
0
 

Author Comment

by:makunag
ID: 8071466
CEHJ,
Can you please elaborate your comment ? I do not have any GUI interface and don't want to create one. I just want users to be able to change parameters in this file and save it for changes without recompiling, repackaging any thing in jar.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8071506
Have an application called EditProps or something in there. It doesn't have to have a gui. I haven't done this, but it could open a stream on the properties file, edit it and then write it back.
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:makunag
ID: 8071645
I am sorry, I still didn't get you. Yes, I can write properies file by an application no problem, but again I have to have some kind of interface with user to get the values user wants to change in properties file. My application is nothing but a tool, which runs along with some script, do some tests and write a report in user's machine. No user interaction involved. I have properties file like this:
key1 = value1
key2 = value2
...
key9 = value9
Now say user wants to change value9 and value2 with something else and rerun the tool( jar file ) how can they provide these key and values to my application without any interface. Do I have to have a file same as peoperties file
where they can change the values they want, and saving that file will trigger my Editprops ( how ? ) ??????
0
 

Expert Comment

by:hirsu
ID: 8071720
I would not pack properties into the Jar file.

I dont know how you are planning to execute your application, but...

1. if it is self executing jar-file, you can put the property file into the root directory and then execute application with : java -jar app.jar
=> the directory where you are is in classpath

2. If you have a script for executing the application, just simply include the directory containing props into the classpath

--
I hope this helps
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8071794
Obviously packing the properties file into the jar file has advantages and disadvantages. Making it harder to get at is an example of both.

>>
how can they provide these key and values to my application without any interface
>>

Well, they could have a command line interface couldn't they. It's up to you whether you think something like:

Enter the option number you'd like to edit:

1. somekey=someval
2. otherkey=otherval


etc.


is easier than getting them to extract the file from the jar, edit it manually and then re-jar it.
0
 

Author Comment

by:makunag
ID: 8072759
hirsu,
That's what will be the best solution for me I think. There are something like 30 items in properties file and creating command line interface for all of them may be ugly.
I tried to do what you suggested but for some reason it does not work. I am surely doing something wrong, can you tell me what ?

1) In my source code, I am using
props = new Properties();
props.load(this.getClass().getResourceAsStream("myProps.properties"));

2) In users C:\Program Files\MyApp I have three files myApp.bat, myApp.jar and myProps.properties

3) myApp.bat looks like this
set CLASSPATH=C:\Program Files\MyApp;%CLASSPATH%
java -jar myApp.jar
I get null pointer when it tries to load properties file.

Then, I tried to modify batch file like this
java -jar -cp %CLASSPATH%;C:\Program Files\MyApp myApp.jar

and now I get:
Failed to load Main-Class manifest attribute from
Files\CSESMonitor;.;C:\Program

My manifest looks like this:

Manifest-Version: 1.0
Main-Class: test.p1.p2.p3.tool.MyMain

Please help.
Thanks

0
 

Author Comment

by:makunag
ID: 8072779
Correction...
read
and now I get:
Failed to load Main-Class manifest attribute from
Files\CSESMonitor;.;C:\Program
as

and now I get:
Failed to load Main-Class manifest attribute from
Files\MyApp;.;C:\Program
0
 
LVL 2

Expert Comment

by:k_suchdeva
ID: 8073811
what if you try
java -jar myApp.jar test.p1.p2.p3.tool.MyMain


Regards
Khem
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8074796
Well, it will probably be slightly intricate to do, but let's not get into clashing/overlapping problems. Let me know once you've got the executable jar bit running in the normal way before trying out my suggestion.
0
 

Author Comment

by:makunag
ID: 8074839
CEHJ
my jar runs fine as long as i include my properties file in my jar, when I don't include my properties in jar instead put in the directory where my jar is located, problem starts. That's when I get null pointer when it tries to access properties file. I changed class path as in my last post.
Tx
0
 

Expert Comment

by:hirsu
ID: 8078301
Well, it seems to me that you might be loading the property file from incorrect place try adding slahsh in front of the propertyfile name
i.e.

props.load(this.getClass().getResourceAsStream("/myProps.properties"));

this locates the property file into the root-level of the classpath. It should work.
0
 

Expert Comment

by:hirsu
ID: 8078318
btw ... there should not be any need to set the classpath if you run the jar file with:

java -jar jarfile.jar

and your property file is in the same directory where you run your application from.
0
 

Expert Comment

by:hirsu
ID: 8078916
btw ... there should not be any need to set the classpath if you run the jar file with:

java -jar jarfile.jar

and your property file is in the same directory where you run your application from.
0
 

Author Comment

by:makunag
ID: 8079522
hirsu,cehj,k_suchdeva
Soory guys, I tried everything posted here but no luck. hirsu, I tried props.load(this.getClass().getResourceAsStream("/myProps.properties"));
too.

If I do not package my properties file in my jar, it just does not work and get me null pointer when it tries to load properties file.
If I package properties file in my jar its fine but goes back to the original problem. If I run my class directly without jar file, and my properties file in root directory it works.
It seems like that when I run my jar, it looks for all the required files packaged in jar only, not outside jar !!!! seems right ??? If that's true than I cannot keep my properties file outside jar ... is this correct ???

Any more thought ??? Thanks everyone for your thoughts so far..
0
 

Author Comment

by:makunag
ID: 8079525
hirsu,cehj,k_suchdeva
Soory guys, I tried everything posted here but no luck. hirsu, I tried props.load(this.getClass().getResourceAsStream("/myProps.properties"));
too.

If I do not package my properties file in my jar, it just does not work and get me null pointer when it tries to load properties file.
If I package properties file in my jar its fine but goes back to the original problem. If I run my class directly without jar file, and my properties file in root directory it works.
It seems like that when I run my jar, it looks for all the required files packaged in jar only, not outside jar !!!! seems right ??? If that's true than I cannot keep my properties file outside jar ... is this correct ???

Any more thought ??? Thanks everyone for your thoughts so far..
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8079630
>>If that's true than I cannot keep my properties file outside jar ... is this correct ???

No. You can have you properties file outside the jar, as long as the properties file is on your classpath.
0
 

Expert Comment

by:hirsu
ID: 8079869
thats correct CEHJ,

I have no problems locating properties outside jar-file. as the matter of fact I would not put properties file into jar-file.

Where in Jar-file do you have the property file at?
what is the package of the class trying to read the property file?
What JDK do you use?
What is the OS?
0
 

Author Comment

by:makunag
ID: 8083379
Well, I removed package from my code to make it simple.
Now I have
C:\Work\firstClass.java
C:\Work\secondClass.java
C:\Work\firstClass.class
C:\Work\secondClass.class
C:\Work\myProps.properties
C:\Work\mainclass.txt ( test file defining main class for manifest)

From the same directory I create jar as
C:\Work>jar cvf myJar1.jar firstClass.class secondClass.class
C:\Work>jar cmf mainclass.txt myJar1.jar firstClass.class secondClass.class

Now I set class path and try to run
C:\Work>SET CLASSPATH=%PATH%;C:\Work
C:\Work>java -jar myJar1.jar

I get:
java.lang.NullPointerException
        at java.io.Reader.<init>(Reader.java:68)
        at java.io.InputStreamReader.<init>(InputStreamReader.java:96)
        at java.io.InputStreamReader.<init>(InputStreamReader.java:85)
        at java.util.Properties.load(Properties.java:205)

I repeat the above process by including myProps.properties in myJar2.jar and everything is cool.

So, answers to your questions:

property file is with class files;
No Package;
version 1.3
and Windows 2000

Thanks for your help. I believe you guys, that it should be able to access properties file outside jar. There may be problem specific to my machine environment configuration as Rational, WSAD and so many other tools are installed with all JDK pugins etc, with a very long class path. If you cannot notice anything in this, I may have to try out in some other machine. Thanks again.

PS:
I load properties file as props.load(this.getClass().getResourceAsStream("/myProps.properties"));

 
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8083714
C:\Work>jar cmf mainclass.txt myJar1.jar firstClass.class secondClass.class

would be sufficient. In fact

C:\Work>jar cmf mainclass.txt myJar1.jar *.class

would be sufficient.

Don't mix up PATH with CLASSPATH - they're two different things (or certainly should be)

You don't need a CLASSPATH (unless you've got a good reason otherwise) so,


C:\Work>SET CLASSPATH=
C:\Work>java -jar myJar1.jar

This, incidentally, doesn't contradict the statement i made above, as the default classpath includes the current directory

Please post code of where you're trying to access the properties file

0
 

Author Comment

by:makunag
ID: 8087702
CEHJ,
C:\Work>SET CLASSPATH=%PATH%;C:\Work
was a typo in my post, what I was doing is C:\Work>SET CLASSPATH=%CLASSPATH%;C:\Work. Sorry about that confusion.

That's true per last post that I do not need to set classpath, but I was giving a try, unfortunately
C:\Work>SET CLASSPATH=
C:\Work>java -jar myJar1.jar didn't work either.

Here is my code, CSESPostTest is my main class in manifest.
public class CSESPostTest
{

       public static void main(String[] args)
     {
         CSESPostTest csesPost = new CSESPostTest();
         csesPost.execute();
     }
    public CSESPostTest()
    {        
    }
    public void execute()
     {
         try
         {
              this.init();
              this.process();
         }
-----
-----


     private void init() throws IOException
     {    
          props = new Properties();
          props.load(this.getClass().getResourceAsStream("/myProps.properties"));
         
          // Load Values
          urlPrefix = getValue("URL_PREFIX");
          testCase = getValue("TEST_CASE");
          times = Integer.parseInt( this.getValue("NUMBER_OF_ITERATION") );
          isLog = getValue("CREATE_LOG").equalsIgnoreCase("Y");
          }
         
          // ArrayList
          errorList = new ArrayList();
     }
-----
-----

     private String urlPrefix = null;
      private String testCase = null;
      private Properties props = null;
     private int times = 0;
     private boolean isLog;
-----
-----
0
 

Author Comment

by:makunag
ID: 8087719
Ignore
"         }
         
         // ArrayList
         errorList = new ArrayList();
"
in my last post. Typo again, Sorry!!!
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 150 total points
ID: 8087742

>>         props.load(this.getClass().getResourceAsStream("/myProps.properties"));

Ah yes, but the purpose of the exercise at the moment is to load the props file from the same directory *outside* the jar, so just do:


FileInputStream in = new FileInputStream("myProps.properties");

props.load(in);

DON'T forget to close the stream.
 
0
 

Author Comment

by:makunag
ID: 8087887
This Works !! This will work for me but still my question is the same: if my current directory ( where my jar file and properties files are located) is in class path why

"props.load(this.getClass().getResourceAsStream("/myProps.properties"));"

is not working ? Shouldn't it look for the file in classpath, whether inside or outside jar. It can locate it as long as its inside jar file but cannot when its outside, even in the class path. Is it safe to say that I cannot keep my properties file outside jar if I am using getClass().getResourceAsStream, even though directory where properties file reside is in class path ?

per Hirsu's post in the beginning.....
"
props.load(this.getClass().getResourceAsStream("/myProps.properties"));

this locates the property file into the root-level of the classpath. It should work.
"

Thanks a lot for your effort. I am happy, it works !!!
:-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8087924
Instead of

>>props.load(this.getClass().getResourceAsStream("/myProps.properties"));"

try

>>props.load(this.getClass().getResourceAsStream("./myProps.properties"));"

and

>>props.load(this.getClass().getResourceAsStream("myProps.properties"));"
0
 

Author Comment

by:makunag
ID: 8088459
Unfortunately none of these works
>>props.load(this.getClass().getResourceAsStream("./myProps.properties"));"

and

>>props.load(this.getClass().getResourceAsStream("myProps.properties"));"

I will accept your previous post as my answer.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8088493
Thanks. In the meantime i'll try and find out *why* it doesn't work
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8088512
Oh, yes, while i'm checking: your command line, for that to work, should probably be

C:\Work>java -cp myJar1.jar -jar myJar1.jar
0
 

Author Comment

by:makunag
ID: 8088575
Sorry, didn't work either
0
 

Author Comment

by:makunag
ID: 8088610
This is what I learned...

if props.load(this.getClass().getResourceAsStream("/myProps.properties"));
 or props.load(this.getClass().getResourceAsStream("./myProps.properties")); or
props.load(this.getClass().getResourceAsStream("myProps.properties"));
is used to load properties and properties file is outside jar, even though in class path DOES NOT Work. ( At least for me)

If
FileInputStream in = new FileInputStream("myProps.properties");
props.load(in);
is used to load properties and properties file is outside jar, WORKS.

Can't have properties file outside jar if getResourceAsStream is used - Not Clear Why??

Thanks to everyone.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8088706
>>Can't have properties file outside jar if getResourceAsStream is used - Not Clear Why??

My initial experiments confirm this so far. The thing is, if you're going to make this call, why would you want the resource *outside* the jar anyway?

It seems to me this is all working very much intuitively.
0

Featured Post

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

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month12 days, 8 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