cmjanicki
Posts: 6
Joined: Mon May 18, 2009 1:37 am

Dynamically add jars during application startup

I have an application that uses JDBC. It is up to the end-user to supply the necessary JDBC libraries. I would like to tell them, "Just put your JDBC jar in this directory..." and then have my application automatically add those jars at startup. Is that possible today?

In fact it would be *much* nicer for my Advanced Installer setup if I only had to specify my application's "lib" directory. Then the Advanced Installer bootstrapper would recursively search all directories and add all jars to the classpath. (This is basically how I build my Unix shell script bootstrappers for my apps.) As a bonus, it might be nice to have a checkbox fro "recursive", so I might order non-recursive directory searches in a particular order, and maybe even list some jars explicitly (like Advanced Installer currently works.)

Thanks,
Chris

PS: The more I learn about Advanced Installer, the more impressed I am with its ability to juggle so many quirky features. I am also increasingly depressed about the complexities of dealing with Windows, but that's another story. ;-)
GabrielBarbu
Posts: 2146
Joined: Thu Jul 09, 2009 11:24 am
Contact: Website

Re: Dynamically add jars during application startup

Hello Chris,

You can achieve this by doing the following:
1) On the Install Parameters page create a new property called LIBDIR
2) Create the interface necessary for the user to select the lib directory. You can achieve this by creating a browse button. Store the selected folder's path in LIBDIR instead of MY_DIR (the property from the example)
3) On the Environment page, create a new environment variable with these settings:

Code: Select all

Name:                         MYCLASSPATH
Value:                        [LIBDIR];[LIBPATHS]
This will add the library folder and all of its subdirectories to the MYCLASSPATH environment variable
4) On the Custom Actions page, make the InstallInitialize standard action visible by right clicking in the tree->Show Standard Action->Before File Installation->InstallInitialize
5) Now place the following two custom actions under InstallInitialize:
a) A CreateExeProcess custom action with the following in the Action Data field:

Code: Select all

[LIBDIR]>cmd /c dir /s /b /a:d>"[TempFolder]libs.txt"
This will list the directories in the folder selected by the user and write them in the libs.txt file located in the user's temporary folder
b) A VBScript Attached Custom Action with the following code:

Code: Select all

'Holds the path of the directory
Dim directoryListing

directoryListing = Session.Property("CustomActionData")

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(directoryListing, ForReading)

Dim classpath

'initialize classpath
classpath = objFile.ReadLine
  
Do Until objFile.AtEndOfStream
  classpath = classpath & ";" & objFile.ReadLine
Loop

Session.Property("LIBPATHS") = classpath
objFile.Close
The above code needs to be placed in a .vbs file. In this custom action's Action Data field you would have to place "[TempFolder]libs.txt" without the quotes, so that our script knows which file to read the directory structure from. Upon finishing, this custom action will set the LIBPATHS property to the list of directories in the folder selected by the user, separated by ";"

Attached is a sample project that implements this.
Let me know if there is anything else I can help you with.

Regards,
Gabriel
Attachments
sample.zip
(4.99 KiB) Downloaded 406 times
Gabriel Barbu
Advanced Installer Team
http://www.advancedinstaller.com/
cmjanicki
Posts: 6
Joined: Mon May 18, 2009 1:37 am

Re: Dynamically add jars during application startup

Wow, thanks for the very detailed reply! A bit convoluted, but it will definitely solve my problem for now.

Feature Request: I hope someday you might hide most of this infrastructure code as part of your *.EXE bootstraps. I'd love to have the Advanced Installer's GUI just contain a button to "Add Directory Jar/Zip Contents" with an extra checkbox for "Recursively".
GabrielBarbu
Posts: 2146
Joined: Thu Jul 09, 2009 11:24 am
Contact: Website

Re: Dynamically add jars during application startup

Hi Chris,

Thank you for your suggestion. We will take this into consideration.

Regards,
Gabriel
Gabriel Barbu
Advanced Installer Team
http://www.advancedinstaller.com/

Return to “Common Problems”