tia_kamakshi
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
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
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
http://netbeanside61.blogspot.com/2008/11/running-ant-targets-from-netbeans-ide.html
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
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.
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
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
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
   ...
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
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
}
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.
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.
ASKER
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.h
System.out.println(Newslet
This Prints null
System.out.println("UserHo
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.getRe
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.getRe sourceAsSt ream("/New sletterMai lMerge.xml ");
in = NewsletterMain.class.getRe
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
>>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
ASKER
Thanks, I will come back to you by tommorow.
Kind Regards,
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
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
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
ASKER
I am on it now. As I am deploying to the production.
Thanks for waiting...
Kind Regards
Thanks for waiting...
Kind Regards
> As I am deploying to the production.
so it worked?
so it worked?
ASKER
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
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;
}
>>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
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);
> I have kept my AppConfig.properties file where I have my jar file.
did you add the directory to the classpath?
did you add the directory to the classpath?
ASKER
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
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
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.
ASKER
Hi,
I have added folder path to the classpath.
%classpath%;D:\SMEC\Common ;D:\SMEC\C ore;E:\SME C;D:\Progr am Files\Java\jre6\lib;D:\SME C\Outlook; D:\SMEC\Co re;
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
I have added folder path to the classpath.
%classpath%;D:\SMEC\Common
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\NewsletterMai lMerge.jar ;%CLASSPAT H% 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
java D:\SMEC\Core\NewsletterMai
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
ASKER
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.ba
and my contents of runCoreApp.bat file is
java NewsletterMailMerge.jar;%C
Now When I execute I get
D:\SMEC\Core>java NewsletterMailMerge.jar;%c
e;E:\SMEC;D:\Program Files\Java\jre6\lib;D:\SME
ettermailmerge.NewsletterM
Exception in thread "main" java.lang.NoClassDefFoundE
ar;%classpath%;D:\SMEC\Com
Caused by: java.lang.ClassNotFoundExc
;D:\SMEC\Common;D:\SMEC\Co
    at java.net.URLClassLoader$1.
    at java.security.AccessContro
    at java.net.URLClassLoader.fi
    at java.lang.ClassLoader.load
    at sun.misc.Launcher$AppClass
    at java.lang.ClassLoader.load
    at java.lang.ClassLoader.load
Could not find the main class: NewsletterMailMerge.jar;%c
n;D:\SMEC\Core;E:\SMEC;D:\
D:\SMEC\Core>
But When I run my .bat file with code
It works great
java -jar PBMSNewsletterMailMerge.ja
Thanks again for all your co-operation
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
(One line only)
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
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
Let him finish then I wil try and come back to you soon today
Thanks again
ASKER
Many Thanks
:-)
http://ant.apache.org/