Thursday, February 16, 2012

Packaging and Deploying a Desktop Java Application which uses embedded derby database

We have already discussed about creating an embedded derby database .For those who are here for the first time check this  first so that it might be easier.  So before coming to packaging first we will create a desktop application in java with netbeans as an example for checking the packaging issues. 

First we need an embedded db for creating this application .I am not elaborating as we have discussed it in my previous post in detail. It is necessary to have at-least 1 column set as primary key.For example create a database named db_data with 1 table named as table1 with 2 columns col1 and col2. Let col1 be set to primary key. After all this insert 2 rows into it and disconnect the db . If you already have an embedded db create a skelton of it in netbeans and with that create the desktop application. Later while including the db include your desired db file along with exe file.

Creating the sample desktop application

From  File->New Project->under Java->choose Java Desktop Application.
Click next and give a project name for instance 'DeskApp' is our project.
Under Choose application shell  choose database application and click next.
Select the database url you just created and under cloumns to include choose all columns(we have only 2 columns now you can change it according to your need), click next and finish.

Now you have your DeskApp in the projects window. 
Right click on DeskApp and select Clean and build.  After this choose Files where you will see the files and libraries included in your application.  There is a dist folder which contains your jar and lib files .This is created while you build your project.

Normally the jar file is only necessary to launch an application.  But here we have dependent lib folder and the database file which should be included so first we package the lib into the jar folder created so that we  have a single jar and the database file.  For this, you will see a build.xml file under your project .  In this xml  code, just before closing </project> paste the code in the below site.
For reading about this further check this site.

(change the line in the above code to name of your project <property name="store.jar.name" value="DeskApp"/> now that it is DeskApp)

Save your project and right click build.xml->RunTarget->OtherTarget->package for storage as seen in the screen shot in here

project files

Now if you check folders under your project you will find a new store directory where your final jar is stored.  This jar contains both lib and you jar file in a single jar file
This is what we need for packaging.





        Creating an exe file for your jar file

Download and install any converter that does this. I used  Launch4j  for this purpose which is an open source software. Once you have downloaded and installed launch4j start it on your PC. Before beginning, create a NewFolder and copy your jar file (in the store folder of your project) and your db_data file (after disconnecting from netbeans move to the folder containing your embedded db file and copy this file to newly created folder under another folder say DB) into it. If you like to have an icon, add any icon file(.ico format). You can convert it here.
Now you have
     NewFolder
            ->  DeskApp.jar
            ->  DB
Building wrapper
                   ->db_data
            ->Tulips.ico

Once you start your launch4j , under the Basic tab, browse for output file and save it for instance win.exe.(eg ..\NewFolder\win.exe)
 Browse for the jar file you copied just now(DeskApp.jar under NewFolder) in *jar. In the icon field browse and choose your icon. You can leave all other fields empty .
Now jump to the jre tab and fill the minimum jre version say 1.4.0(as you want it to be) and select the Build Wrapper tool on top .
Testing wrapper

Once you select this give a name say config for the file to be saved. When your log says succesfully created the file select the Test wrapper next to Build Wrapper .Now you have your exe file in the folder you selected

       NewFolder
                 ->  DeskApp.jar
                 ->  DB
                          ->db_data
                  ->Tulips.ico
                  ->win.exe
                                                                 ->config                                                                                        

Creating an installer for your application 

There might be easier and efficient ways of doing this but right now I used the Inno setup compiler .I think its simpler for beginners.

Once you have this installed in your system start it. First you will find a welcome window under that choose only Create a new script file using the script wizard and then click OK





The Inno set up Script wizard appears on this.
Click next and fill the details of your application your company profile etc where only name of your application and its version is necessary.

On next page specify where you want your application to be installed (normally it will be in program files) you can also give a folder name for your application (which is recommended) and check allow user to change folder.
Next you have to choose the exe file you created just now using launch4j.
Below that you will see Add folder .Browse and add the main DB folder .Select yes for should subfolders be included?






In application documentation you can include text files for license etc(not mandatory).
Choose your language and click next .
There you can choose a folder where you want your set up file to reside under Custom compiler output folder and the icon file used for exe file(you can have another image).
Finish the wizard.






Choose yes when it asks would you like to compile the new script now ? and save the script before compiling .This can be useful later if you have to change the script.


After the compilation is done you can check the setup folder. Double click the file and you can see your installer running.After all installation is done you will see the desktop icon of your application . You can double click and run your application from desktop (For windows 7 right click on icon and run as administrator)  In control panel you can also find your application under uninstall programs ,from where you can remove it completely


This may or may not be a  professional way of approaching this issue...   .just found a small thing and was excited to share with all. Hope this was helpful in some way and you got a slight idea about deployment. Looking forward to your feedback's and comments.

Check out my next post for deploying a desktop application on a system without java installed
Software License for Launch4j
Software License for InnosetUp


37 comments:

Basanta Thapa said...

Thank you Aparna, a long awaited article. Thank you so much. In addition to it, if we can make serial key based installers, then it would be a lot helpful to build a commercial application installer. That's just a suggestion, if you can manage time then that would too help the beginners like us.

aparna said...

Thank you for your valuable feedback.
Once I finish experimenting on serial key based installers will definitely include it as my upcoming post.

Basanta Thapa said...

Thank you Aparna, that post will be highly awaited.

Anonymous said...

Hey Aparna,thank you so much.My application working on local machine but not on another machine.It is throwing exception database not found.please help me!

aparna said...

Are you sure that you packed your database file together with the exe?

Anonymous said...

My db name is MyDB.So I included that MyDB folder,jar file in store folder to new folder.is it right?

aparna said...

You can give any folder name but make sure you keep the folder hierarchy.
That is inside the New Folder make another folder named Db and inside that place your database. Check if its right .. If the system where you are installing doesn't have java then see my next post to package jre along with exe.

aparna said...

Also check if you have changed your system specific path to "jdbc:derby:MYDB" just before building your application.

Anonymous said...

if I change path to jdbc:derby:MyDB then while testing wrapper it throws exception
database MyDB not found

aparna said...

GMT Thread[AWT-EventQueue-0,6,main] java.io.FileNotFoundException: derby.log (Access is denied) This is what you get right ? just ignore and continue

Anonymous said...

I m getting following exception java.sql.SQlException:database "MyDB" not found!

Anonymous said...

I think Im getting this exception coz I changed path from jdbc:derby:c:/...netbeans/7.1/derby/MyDB to jdbc:derby:MyDB

aparna said...

But that will not work in another system. So its your folder hierarchy issue. I just did it once more here and its fine.

Anonymous said...

Actually after changing path java program also throws that database not found exception.

aparna said...

When you have to run your program from ide you should not change the path because it points to database in jdbc:derby:c:/...netbeans/7.1/derby/MyDB folder in your system and that is correct. What i suggested is once all your checking is done, change the path to jdbc:derby:MyDB and after that, don't run your program but select clean and build. Then package and store the jar. After this make a folder and inside that place your jar from store folder and database as mentioned in the post. Strictly keep the hierarchy.

Anonymous said...

You mentioned "In this xml code, just before closing paste the code in the below site"
where is this code??

Anonymous said...

Thanks a lot. This is really helpful .....
I think you are referring to this link http://www.oracle.com/technetwork/articles/javase/index-139904.html

Anonymous said...

I followed all your steps.But jar file made in store folder can't find javadb connection.What's wrong?

Anonymous said...

Hey Aparna,I followed your previous post and made my project now if i follow all steps of this post then db connection is not made.
if i do the same with java database type project then it is making connection.But i need to do all things with my previous project.
Can you please give me some solution.

Anonymous said...

Hey Aparna,I followed your previous post and made my project now if i follow all steps of this post then db connection is not made.
if i do the same with java database type project then it is making connection.But i need to do all things with my previous project.
Can you please give me some solution.

Anonymous said...

Hey Aparna,I followed your previous post and made my project now if i follow all steps of this post then db connection is not made.
if i do the same with java database type project then it is making connection.But i need to do all things with my previous project.
Can you please give me some solution.

SAM said...

Hi Aparna,

This is very crisp and useful post. At least for my case, I prefer to code in java mainly because I like to see my Apps running in different Operating Systems. Can you suggest me how do I wrap it for Linux flavor operating systems ?

Pan Ma said...

Hey guys,
I got the same problem with you guys:db not found for the exe.
I found you have to put the whole db folder with the exe file together so that it can find it.
Hope this can help.
Great blog!!

Anonymous said...

Thank you so much for this post.It was really useful to me. I was searching for an easy and efficient way to deploy my Java applications for the last couple of days.
You are really a geek.Keep going,All the best.

Anonymous said...

I want to Pack my database with my java application.
I have used derby database with client driver.
But I'm Not Able To The Packaging thing along with database can you help? pleaseeeeeeeeeeeeeeeeeeeeeeeee i will be really thank full to you

David Allison said...

Nice post. I have bookmarked you to check your new stuff. Thanks for sharing this useful post, Please keep sharing more...

Application Packaging
Sccm Deployment
Application Compatibility Toolkit

Thomson said...

Aparna.. You have no idea how much you've helped me. I finished my project on time coz of you. And whatever you do please do not stop this. Helps a ton to people like me.
Thanks again!

Barbie Chiu said...

I am so glad to be given a chance to read your wonderful article. Im looking forward to read more of your works and posts. You did a good job! Try to visit my site too and enjoy.

triciajoy.com

www.triciajoy.com

Debjit Ghosh said...

Thank you for your post.

I followed your tutorial and got some success, but still some more help is required.

I am working with Netbeans 7.4 IDE and there is no PACKAGE FOR STORAGE on run target. So I created an .exe file with the jar created by IDE and when I run it, it only shows the splash and after that no JFrame is created.

I am also running a Compatibility issue. When I am installing in win XP it install properly with splash only, but when I am installing in win7, it install in a compatible mode.

Can you please provide me a solution to fix it? When I Click the jar file it works fine (splash & log in page both appears).

niazi said...

This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

Love from

Dheeraj Upadhyay said...

@Aparna
Actually the site name and link you provide for snapshot is not working.
by clicking on link I am redirecting to http://www.oracle.com/technetwork/java/index.html link.
Please verify it and update your blog.

Naviya Nair said...

I have read your blog its very attractive and impressive. I like it your blog.

Java Online Training Java EE Online Training Java EE Online Training Java 8 online training Core Java 8 online training

Java Online Training from India Java Online Training from India Core Java Training Online Core Java Training Online Java Training InstitutesJava Training Institutes

Naviya Nair said...

I have read your blog its very attractive and impressive. I like it your blog.

Java Online Training Java EE Online Training Java EE Online Training Java 8 online training Core Java 8 online training

Java Online Training from India Java Online Training from India Core Java Training Online Core Java Training Online Java Training InstitutesJava Training Institutes

DataBytes Bangalore said...

Enroll for Java Training in Bangalore with DataBytes; best Java Coaching Institutes in Bangalore.

Indu Priya said...

Very Useful Blog I really Like this blog and i will refer this blog...
And i found a some usefull content for Online training check It out .

Karthika Shree said...

This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
Java Training in Chennai

David Allison said...

Really informative article. Please Visit:

windows 7 to 10 migration
16-bit application
Windows Packaging