Solved

Creating Jar file without including configurations file

Posted on 2009-05-09
34
385 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
[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
  • 13
  • 10
  • 8
  • +1
34 Comments
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 24343415
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
ID: 24343420
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
ID: 24343436
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tia_kamakshi
ID: 24344423
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
ID: 24344574
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
ID: 24344614
>>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
ID: 24344666
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
ID: 24346011
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
ID: 24346113

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
ID: 24346138
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
ID: 24347162
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
ID: 24347642
Thanks, I will come back to you by tommorow.

Kind Regards,
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24347663
>>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
ID: 24347786
> 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
ID: 24364660
I am on it now. As I am deploying to the production.

Thanks for waiting...

Kind Regards
0
 
LVL 92

Expert Comment

by:objects
ID: 24370337
> As I am deploying to the production.

so it worked?

0
 

Author Comment

by:tia_kamakshi
ID: 24376558
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24376966
>>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
ID: 24377029
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
ID: 24380011
> 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
ID: 24384386
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
ID: 24384796
>>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
ID: 24391150
No, just adding the directory to the classpath will be all you need.

0
 

Author Comment

by:tia_kamakshi
ID: 24395113
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
ID: 24395137
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
ID: 24395139
(a relative path)
0
 
LVL 92

Expert Comment

by:objects
ID: 24395237
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
ID: 24439797

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
ID: 24439824
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
ID: 24439828
(One line only)
0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 24439894
> 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
ID: 24440218
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
ID: 31579755
Many Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24493235
:-)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

734 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