Solved

Creating Jar file without including configurations file

Posted on 2009-05-09
34
369 Views
Last Modified: 2013-12-14
Hi,

I have created a java appilication using Netbeans

When I create a jar file using netbeans, jar file includes all the xml files and properties files in it. So if we wanted to change any configuration then we have to create a jar file again.

I donot wanted to create jar file which includes conf xmls and application config properties file

How do we fix this

Please guide

Many Thanks
0
Comment
Question by:tia_kamakshi
  • 13
  • 10
  • 8
  • +1
34 Comments
 
LVL 20

Expert Comment

by:ChristoferDutz
Comment Utility
Have you thought of building your jars using Apache Ant? This tool is really great for tasks like that.
http://ant.apache.org/
0
 
LVL 20

Expert Comment

by:ChristoferDutz
Comment Utility
Ok ... as Netbeans seems to use Ant anyway ... here a more detailed Link to the Ant/Netbeans integration
http://netbeanside61.blogspot.com/2008/11/running-ant-targets-from-netbeans-ide.html
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You can keep the config files external to the jar, say in user.home or have the best of both worlds and have them initially in the jar as well
0
 

Author Comment

by:tia_kamakshi
Comment Utility
HI,

Thanks for your responses.

1. I have used ant in one of my project before for building application. But I have never configured. I may need your help

I have downloaded ant from  http://ant.apache.org/bindownload.cgi

I don't know how to install ant and configure it in netbeans

2. Trying to understand CEHJ comment.

My main class (NewsletterMain.java ) exists in folder newslettermailmerge
and my config files(AppConfig.properties, NewsletterMailMerge.xml) is one folder up in src folder

You can see my dir /file structure below

When I create jar file, all config files are in jar file only.

You mean I can place my config files where I have kept my jar files. And If I change my config file this will work. Is it what you mean

Please guide

Many Thanks.

C:\Documents and Settings\user\My Documents\NetBeansProjects\NewsletterMailMerge\src
 

29/04/2009  11:44    <DIR>          .

29/04/2009  11:44    <DIR>          ..

08/05/2009  17:36               448 AppConfig.properties

09/03/2009  13:14    <DIR>          com

10/03/2009  15:58            14,848 employees.xls

29/04/2009  11:44            43,008 HTMLEncoding.xls

11/03/2009  14:00             3,507 log4j.xml

16/04/2009  13:09    <DIR>          newslettermailmerge

08/05/2009  14:21            46,445 NewsletterMailMerge.xml

09/03/2009  14:17    <DIR>          Templates

               5 File(s)        108,256 bytes

               5 Dir(s)  19,385,634,816 bytes free
 
 

 Directory of C:\Documents and Settings\user\My Documents\NetBeansProjects\NewsletterMailMerge\src\newslettermailmerge
 

16/04/2009  13:09    <DIR>          .

16/04/2009  13:09    <DIR>          ..

08/05/2009  17:49            14,762 NewsletterMain.java // Main Jav File

               1 File(s)         14,762 bytes

               2 Dir(s)  19,385,630,720 bytes free

Open in new window

0
 
LVL 20

Expert Comment

by:ChristoferDutz
Comment Utility
Do I understand it right, that AppConfig.properties and NewsletterMailMerge.xml are config files of Netbeans, that are not used in the program you are vreating?

Then I would certainly put these files up one directory so they reside parallel to the src directory:

AppConfig.properties
NewsletterMailMerge.xml
src/
     com
     log4j.xml
     ...
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>I don't know how to install ant and configure it in netbeans

You don't need to. Ant is already an integral part of NetBeans. It's whole build process uses Ant
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
After making a resources directory in your root, this is the sort of strategy you could use for the user files:
File f = new File(System.getProperty("user.home"), "NewsletterMailMerge.xml");

if (f.exists()) {

	// Load it

	in = new FileInputStream(f);

}

else {

	in = NewsletterMain.class.getResourceAsStream("/resources/NewsletterMailMerge.xml"); 

	// Now read from InputStream 'in' and THEN write it to the file that was first tried above

}

Open in new window

0
 
LVL 92

Expert Comment

by:objects
Comment Utility
you need to move your xml files out of the src directory (as ChristoferDutz mentions), otherwise they will get included in your jar

Did you have any preference to where to store the config files?

With your properties file you could use the following contructor to load
http://java.sun.com/javase/6/docs/api/java/util/Properties.html#Properties(java.util.Properties)
that allows you to have defaults, so you could for example load a properties file from your jar as the defaults and pass it to the constructor when you load the external properties.

0
 

Author Comment

by:tia_kamakshi
Comment Utility

Thanks for your replies

I wanted that my app.config file to reside from a folder where I put my jar file

My main config file is AppConfig.properties file

Now


1. I have printed below lines

String strUserHome = System.getProperty("user.home");

System.out.println(NewsletterMain.class.getResourceAsStream("/resources/NewsletterMailMerge.xml"));

This Prints null

System.out.println("UserHome is:"+strUserHome);

This Prints: UserHome is:C:\Documents and Settings\user


That means I need to put my NewsletterMailMerge.xml in user home directory that is C:\Documents and Settings\user

But I wanted to put my .properties file at the location where I put my application.jar file

I have already given my folder structure

Yes, I don't have "resources" folder in my application

Then If I write
in = NewsletterMain.class.getResourceAsStream("/resources/NewsletterMailMerge.xml");

it will always give me null value


2. AppConfig.properties and NewsletterMailMerge.xml  are not netbeans config files but it is the config file created by me

Now what code I should write in my program so that these config files to be picked up from whatever folder I deploy my jar file in production server

I mean In production server whereever I keep my .jar file from there only these config files should be read and can be modified

and my jar file should not include the config file itself so that it is hard to modify properties in production server as required


Please guide

Many Thanks
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Try adding the directory that contains your jar to the classpath and use:

in = NewsletterMain.class.getResourceAsStream("/NewsletterMailMerge.xml");

0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Putting to put the configuration files in the same directory as the one you deploy your jar to is not really a safe strategy: it might not be writable. user.home is always writable.

>>Yes, I don't have "resources" folder in my application

You need to make one and put your resource files in there, then you won't have a problem
0
 

Author Comment

by:tia_kamakshi
Comment Utility
Thanks, I will come back to you by tommorow.

Kind Regards,
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>is not really a safe strategy: it might not be writable.

Actually it's not likely to be unwritable but using the home directory is more predictable and will allow your jar to be (re)located anywhere
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> Thanks, I will come back to you by tommorow.

no worries, let me know how you go
you can put the resources in whatever directory you want really. Just be sure to add the appropriate directory to your class path so the code I posted above will find them correctly
0
 

Author Comment

by:tia_kamakshi
Comment Utility
I am on it now. As I am deploying to the production.

Thanks for waiting...

Kind Regards
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> As I am deploying to the production.

so it worked?

0
 

Author Comment

by:tia_kamakshi
Comment Utility
Not yet still struggling.

I have kept my AppConfig.properties file where I have my jar file.

It is not picking properties file. It is working based on the properties file included jar file

I wanted that we should be able to update AppConfig.properties file from the same folder

Please guide

Many Thanks
public Properties getProperties()

    {

        Properties properties = new Properties();
 

        try {

        //        properties.load(new FileInputStream("/AppConfig.properties"));

            InputStream in = getClass().getResourceAsStream("/AppConfig.properties");

            properties.load(in);

            in.close();
 

                String strPropertyValue = properties.getProperty("DBConnectionString");

                //mL.info("Property Read: "+strPropertyValue);

        } catch (IOException e) {

            mL.error("Property file not read: " ,e);

            System.out.println("Error in reading property file");

        }
 

        return properties;

    }

Open in new window

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>I have kept my AppConfig.properties file where I have my jar file.

That's not useful really. The jar doesn't really know what directory it's in so you can't load files with ease. That's why it's better to have it in a known readable location such as user.home

It also knows nothing of classpaths outside the jar essentially, so if you load an external resource load it as a File. See http:#24344666
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
IF you could be certain that your jar was always going to be run from its own directory, THEN you could do the following if the config file were with it:


InputStream in = new FileInputStream("AppConfig.properties");

props.load(in);

Open in new window

0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> I have kept my AppConfig.properties file where I have my jar file.

did you add the directory to the classpath?

0
 

Author Comment

by:tia_kamakshi
Comment Utility
Thanks, I am doing some changes today in dvelopment again and re- deploy my code on production. Lets see how it works this time.

I have 3 different application running. In my classpath I have given base directory path.

I cannot put my app.config in my home directory. This has to be in the location where I have my jar file

I have done the changes you asked me. Lets see how it goes.

Is there any other something specific you want me to try other than above solutions


I will come back to you after re-deploying my app

Many Thanks again

Kind Regards
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>This has to be in the location where I have my jar file

If you make sure the jar is run from its own directory then

File f = new File("AppConfig.properties");

can be used to avoid confusion with any you might like to load internally from the jar as a default
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
No, just adding the directory to the classpath will be all you need.

0
 

Author Comment

by:tia_kamakshi
Comment Utility
Hi,

I have added folder path to the classpath.

%classpath%;D:\SMEC\Common;D:\SMEC\Core;E:\SMEC;D:\Program Files\Java\jre6\lib;D:\SMEC\Outlook;D:\SMEC\Core;

But it doesn't worked

Please guide

D:\SMEC\Core>dir
 Volume in drive D is App
 Volume Serial Number is 50DD-4825

 Directory of D:\SMEC\Core

15/05/2009  14:10    <DIR>          .
15/05/2009  14:10    <DIR>          ..
15/05/2009  14:11               442 AppConfig.properties
10/03/2009  15:58            14,848 employees.xls
29/04/2009  11:44            43,008 HTMLEncoding.xls
12/05/2009  15:02    <DIR>          lib
15/05/2009  00:01    <DIR>          Log
12/05/2009  17:38             3,742 log4j.xml
12/05/2009  18:10           251,904 NewsletterMailMerge.jar
14/05/2009  16:42             8,946 NewsletterMailMerge.xml
12/05/2009  18:10             1,462 README.TXT
12/05/2009  15:26                44 runCoreApp.bat
               8 File(s)        324,396 bytes
               4 Dir(s)  10,777,468,928 bytes free


Kind Regards
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You're running the app from a jar are you not? If so, the path must be inside the manifest
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
(a relative path)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
how are you running it? should be something like

java D:\SMEC\Core\NewsletterMailMerge.jar;%CLASSPATH% ClassName

or add the jar to CLASSPATH, then you don't need the classpath anymore
a batch file can be good for setting classpath and starting it up

0
 

Author Comment

by:tia_kamakshi
Comment Utility

Thanks for your responses and very sorry for late response.

I was waiting to deploy my things again to production server to test this again

Anyway,

1. How can I change path in menefiest file for different differnt environment

I mean in development mode and UAT mode, Production mode etc

Also, When I create a executable jar file from netbeans it uses path from its own so that it should run anywhere and include all xml file, properties file in it directly

Then I should I modify Menefiest file

2. When I run my .bat file

D:\SMEC\Core>runCoreApp.bat

and my contents of runCoreApp.bat file is

java NewsletterMailMerge.jar;%CLASSPATH% pbmsnewslettermailmerge.NewsletterMain golive


Now When I execute I get

D:\SMEC\Core>java NewsletterMailMerge.jar;%classpath%;D:\SMEC\Common;D:\SMEC\Cor
e;E:\SMEC;D:\Program Files\Java\jre6\lib;D:\SMEC\Outlook;D:\SMEC\Core; pbmsnewsl
ettermailmerge.NewsletterMain golive
Exception in thread "main" java.lang.NoClassDefFoundError: NewsletterMailMerge/j
ar;%classpath%;D:\SMEC\Common;D:\SMEC\Core;E:\SMEC;D:\Program
Caused by: java.lang.ClassNotFoundException: NewsletterMailMerge.jar;%classpath%
;D:\SMEC\Common;D:\SMEC\Core;E:\SMEC;D:\Program
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: NewsletterMailMerge.jar;%classpath%;D:\SMEC\Commo
n;D:\SMEC\Core;E:\SMEC;D:\Program.  Program will exit.

D:\SMEC\Core>


But When I run my .bat file with code

It works great

java -jar PBMSNewsletterMailMerge.jar golive

Thanks again for all your co-operation
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
Comment Utility
That should be
D:\SMEC\Core>java -classpath NewsletterMailMerge.jar;%classpath%;D:\SMEC\Common;D:\SMEC\Cor

e;E:\SMEC;D:\SMEC\Outlook;D:\SMEC\Core; pbmsnewslettermailmerge.NewsletterMain golive

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
(One line only)
0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
Comment Utility
> 1. How can I change path in menefiest file for different differnt environment

you would use command line, not the manifest

> java NewsletterMailMerge.jar;%CLASSPATH% pbmsnewslettermailmerge.NewsletterMain golive

you missed the -classpath param should be:

java -classpath NewsletterMailMerge.jar;%CLASSPATH% pbmsnewslettermailmerge.NewsletterMain golive

0
 

Author Comment

by:tia_kamakshi
Comment Utility
Many Thanks, Someone is on Production server using RDP.

Let him finish then I wil try and come back to you soon today

Thanks again
0
 

Author Closing Comment

by:tia_kamakshi
Comment Utility
Many Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

762 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

6 Experts available now in Live!

Get 1:1 Help Now