Startup scripts without a PRODUCT_HOME variable

Another old and simple subject I should have posted a long time ago:

A typical command line tool implemented in Java contains a startup script (actually many of them - it's run everywhere only once you get it running...) for configuring and starting a JVM for the tool.

Some tools require a PRODUCT_HOME environment variable to be able to find the classes and other files of the tool.

This isn't necessary, the script can detect its own location and deduce the tool location from it.

Assuming the startup script is under a subdirectory of PRODUCT_HOME, for example bin, PRODUCT_HOME can be deduced like this:

First the bash version:

SCRIPT_HOME=$(dirname "$0")
PRODUCT_HOME="$SCRIPT_HOME/.."

And the Windows version, as documented by puzzlehacker, among others; I don't know which Windows versions support this (and luckily it's not really my problem):

set SCRIPT_HOME=%~dp0
set PRODUCT_HOME=%SCRIPT_HOME%\..

Originally published on 2008-02-05 at http://www.jroller.com/wipu/entry/startup_scripts_without_a_home under category Scripting