I’m quite new to java and have a few doubts in setting the classpath.
I’ve done my home work before asking the question in this forum. Tried googling and refered other forums, but did not find
what I was searching for.
So, here it goes.
- I’m developing a Java application which needs external jars to run properly.
- These external jars are available, if and only if a custom application ‘XYZ’ is installed in the system.
- Because of licensing issues, I do not want to pack the XYZ application jars in my application.
- What my application does is – check if application XYZ is present in the destination host, and runs if it is available.
Else logs an error stating XYZ is required to run the program and exits.
Now comes the main part…
- XYZ application is available for various platforms – Linux / Windows / Solaris / etc.
- XYZ can be customized during installation to be installed to custom directory.
say – D:/XYZ, or D:/apps/XYZ in Windows, /apps/XYZ or /apps/admin/XYZ in linux, solaris etc. - After installing the XYZ application, the jar files will be present in the directory /jars in which XYZ was installed.
For example – D:/XYZ/jars, /apps/XYZ/jars in *nix. - When the application XYZ is installed in any platform, a new environment variable XYZ_LIB_PATH is created and pointed to
the respective folder where the jars are present – for example XYZ_LIB_PATH=D:/XYZ/jars is created when the application
XYZ is installed to D:/XYZ. In the same way, XYZ_LIB_PATH=/apps/XYZ/jars would be created if the application XYZ is installed
in a linux machine.
Now what I want to do is..
I want to refer my application to the environment variable – XYZ_LIB_PATH and pick up all the jar files present in the directory
pointed by the XYZ_LIB_PATH environment variable of the platform.
By doing this, I need not worry about looking for jar files in each machine when my application is installed. It can just look
into the directory pointed by the XYZ_LIB_PATH variable and continue to run by loading the required jars.
The reason behind this is – since the application XYZ is customizable, each user may have installed it in a different directory
and my application needs to find out where XYZ has been installed. Instead if I use the environment variable, I can get the
required jars easily for my application by looking into the path set for the XYZ_LIB_PATH variable. The user cannot change the
environment variable and hence this variable remains constant across all platforms and I can get a valid path for the jars.
Is there any way to accomplish this in java?
Thanks,
Vinay S
Just provide a startup script for your app:
(above example is for Windows. The same can be done on Unix).
If needed, this script could detect if the XYZ_LIB_PATH environment variable is set and gracefully exit if it isn’t: