Development - Set environment script

on Tuesday, 15 May 2012
To run many of the tools you find in Java development you first have to ensure the environment you're working in is configured correctly - this is often the command line for Windows or shell for Linux. For example when you want to run the 'javac' program from the command line, your computer needs to knows the path to that executable file.

**NOTE** it is possible to make environment variables permanent on a machine so that they only ever need to be set once. This can be dangerous for several reasons:

a) If you set a faulty path you could crash your machine and the change may be irreversible.
b) If you switch machine you will have to try and remember all the changes you made.

Here enters the setenv script!!


@rem *************************************************************************
@rem This script is used to set up your environment for development.
@rem It sets the following variables:
@rem
@rem
@rem JAVA_HOME - Location of the version of Java that you want to use for development.
@rem This variable must point to the root directory of a JDK
@rem installation and will be set for you by the installer.
@rem PATH - Adds the JDK and tool directories to the system path.
@rem CLASSPATH - Adds the JDK and tool jars to the classpath.
@rem
@rem Other variables that takes are:
@rem
@rem PRE_CLASSPATH - Path style variable to be added to the beginning of the
@rem CLASSPATH
@rem POST_CLASSPATH - Path style variable to be added to the end of the
@rem CLASSPATH
@rem PRE_PATH - Path style variable to be added to the beginning of the
@rem PATH
@rem POST_PATH - Path style variable to be added to the end of the PATH
@rem
@ECHO off
TITLE Environment Setup
REM Set user-defined variables.
ECHO Clearing any currently set project-specific environment variables...
SET JAVA_HOME=
SET GRADLE_HOME=
SET GROOVY_HOME=
SET PRE_CLASSPATH=
SET POST_CLASSPATH=
SET PRE_PATH=
SET POST_PATH=
ECHO Setting project-specific environment variables...
SET TOOLS_DIR=D:\tools
SET JAVA_HOME=%TOOLS_DIR%\java\1.7.0_07
SET GRADLE_HOME=%TOOLS_DIR%\gradle\1.6
SET GROOVY_HOME=%TOOLS_DIR%\groovy\2.1.6
SET PRE_CLASSPATH=
SET POST_CLASSPATH=
SET PRE_PATH=D:\Apps\Firefox;%JAVA_HOME%\bin;%GRADLE_HOME%\bin;%GROOVY_HOME%\bin
SET POST_PATH=
REM Get PRE and POST environment
IF NOT "%PRE_CLASSPATH%" == "" SET CLASSPATH=%PRE_CLASSPATH%;%CLASSPATH%
IF NOT "%POST_CLASSPATH%" == "" SET CLASSPATH=%CLASSPATH%;%POST_CLASSPATH%
IF NOT "%PRE_PATH%" == "" SET PATH=%PRE_PATH%;%PATH%
IF NOT "%POST_PATH%" == "" SET PATH=%PATH%;%POST_PATH%
REM Check that java is where we expect it to be
:checkJava
IF NOT EXIST "%JAVA_HOME%\bin\java.exe" (
ECHO The JDK wasn't found in directory %JAVA_HOME%.
ECHO Please edit this script so that the JAVA_HOME
ECHO variable points to the location of your JDK.
ECHO Your environment has not been set.
) ELSE (
ECHO The file exists
)
GOTO finish
:finish
view raw setenv.bat hosted with ❤ by GitHub
This is a very simple script to set up the Windows command line interface to use Java. Copy this into your favourite text editor and save it as setenv.bat. Now, edit the script to include your Java install then open a command line prompt. Once open type "set" and run. A series of value/variables pairs should be displayed but JAVA_HOME should not be set.

Now, run "setenv.bat" and "set". You should now notice both the PATH and JAVA_HOME variables have been set.

And here is the Linux version:

# *************************************************************************
# This script is used to set up your environment for development.
# It sets the following variables:
#
#
# JAVA_HOME - Location of the version of Java that you want to use for development.
# This variable must point to the root directory of a JDK
# installation and will be set for you by the installer.
# PATH - Adds the JDK and tool directories to the system path.
# CLASSPATH - Adds the JDK and tool jars to the classpath.
#
# Other variables that takes are:
#
# PRE_CLASSPATH - Path style variable to be added to the beginning of the
# CLASSPATH
# POST_CLASSPATH - Path style variable to be added to the end of the
# CLASSPATH
# PRE_PATH - Path style variable to be added to the beginning of the
# PATH
# POST_PATH - Path style variable to be added to the end of the PATH
#
#*****************************************************************************
# Set user-defined variables.
echo "Clearing any currently set project-specific environment variables..."
export JAVA_HOME=
export ANT_HOME=
export ANT_OPTS=
export PRE_CLASSPATH=
export POST_CLASSPATH=
export PRE_PATH=
export POST_PATH=
echo "Setting project-specific environment variables..."
export JAVA_HOME=u01/apps/Java/jdk1.5.0_09
export ANT_HOME=
export ANT_OPTS=
export PRE_CLASSPATH=
export POST_CLASSPATH=
export PRE_PATH=
export POST_PATH=;%JAVA_HOME%/bin
# Get PRE and POST environment
if [ ! -z "${PRE_CLASSPATH}" ]; then
CLASSPATH="${PRE_CLASSPATH}${CLASSPATHSEP}${CLASSPATH}"
fi
if [ ! -z "${POST_CLASSPATH}" ]; then
CLASSPATH="${CLASSPATH}${CLASSPATHSEP}${POST_CLASSPATH}"
fi
if [ ! -z "${PRE_PATH}" ]; then
PATH="${PRE_PATH}${PATHSEP}${PATH}"
fi
if [ ! -z "${POST_PATH}" ]; then
PATH="${PATH}${PATHSEP}${POST_PATH}"
fi
# Check that java is where we expect it to be
elif [ ! -d "${JAVA_HOME}/bin" ]; then
echo
echo "The JDK wasn't found in directory ${JAVA_HOME}."
echo "Please edit this script so that the JAVA_HOME"
echo "variable points to the location of your JDK."
echo CLASSPATH=${CLASSPATH}
echo
echo PATH=${PATH}
echo
echo "Your environment has been set."
fi
view raw setenv.sh hosted with ❤ by GitHub
These scripts I have provided are very simple and only currently set up your Java environment. You can use the same principles to set up your build tools or any other third party tools you may wish to run from the command line.

0 comments:

Post a Comment