Link to home
Create AccountLog in
Avatar of tia_kamakshi
tia_kamakshiFlag for United Arab Emirates

asked on

Creating Jar file without including configurations file

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
Avatar of ChristoferDutz
ChristoferDutz
Flag of Germany image

Have you thought of building your jars using Apache Ant? This tool is really great for tasks like that.
http://ant.apache.org/
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
Avatar of CEHJ
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
Avatar of tia_kamakshi

ASKER

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

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
     ...
>>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
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

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.


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
Try adding the directory that contains your jar to the classpath and use:

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

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
Thanks, I will come back to you by tommorow.

Kind Regards,
>>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
> 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
I am on it now. As I am deploying to the production.

Thanks for waiting...

Kind Regards
> As I am deploying to the production.

so it worked?

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

>>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
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

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

did you add the directory to the classpath?

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
>>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
No, just adding the directory to the classpath will be all you need.

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
You're running the app from a jar are you not? If so, the path must be inside the manifest
(a relative path)
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


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
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
(One line only)
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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
Many Thanks
:-)