Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 455
  • Last Modified:

Shall I include my properties file in Jar ?

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
makunag
Asked:
makunag
  • 14
  • 11
  • 5
  • +1
1 Solution
 
CEHJCommented:
Why not write a small application to edit this file and put it in the same jar?
0
 
makunagAuthor Commented:
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
 
CEHJCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
makunagAuthor Commented:
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
 
hirsuCommented:
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
 
CEHJCommented:
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
 
makunagAuthor Commented:
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
 
makunagAuthor Commented:
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
 
k_suchdevaCommented:
what if you try
java -jar myApp.jar test.p1.p2.p3.tool.MyMain


Regards
Khem
0
 
CEHJCommented:
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
 
makunagAuthor Commented:
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
 
hirsuCommented:
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
 
hirsuCommented:
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
 
hirsuCommented:
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
 
makunagAuthor Commented:
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
 
makunagAuthor Commented:
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
 
CEHJCommented:
>>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
 
hirsuCommented:
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
 
makunagAuthor Commented:
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
 
CEHJCommented:
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
 
makunagAuthor Commented:
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
 
makunagAuthor Commented:
Ignore
"         }
         
         // ArrayList
         errorList = new ArrayList();
"
in my last post. Typo again, Sorry!!!
0
 
CEHJCommented:

>>         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
 
makunagAuthor Commented:
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
 
CEHJCommented:
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
 
makunagAuthor Commented:
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
 
CEHJCommented:
Thanks. In the meantime i'll try and find out *why* it doesn't work
0
 
CEHJCommented:
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
 
makunagAuthor Commented:
Sorry, didn't work either
0
 
makunagAuthor Commented:
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
 
CEHJCommented:
>>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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 14
  • 11
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now