Download Java Applications and the Environment Variables

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Environment Variables For Java Applications
PATH, CLASSPATH, JAVA_HOME
What are Environment Variables?
Environment variables are global system variables accessible by all the processes running under
the operating system. Environment variables are useful to store system-wide values such as the
directory paths to search for the executable programs, the OS version, and the location of
Windows binaries. Examples of environment variables are:
 COMPUTENAME, USERNAME: stores the computer and current user name.
 OS: the operating system.
 SystemRoot: the system root directory.
 PATH: stores a list of directories for searching executable files.
A process could access all the environment variables, it could also maintain its own local
variables, which is available to itself only.
How to Set or Change a Variable
Take note that variables in CMD are NOT case-sensitive (because the legacy DOS are NOT casesensitive).
Display Variables: To list all the variables and their values, start a CMD shell and issue
command "SET". To display a particular variable, use command "SET varname". For examples,
// Display all the variables (in NAME=VALUE pairs)
prompt> SET
COMPUTERNAME=xxxxxxx
OS=xxxxxxx
PATH=xxxxxxx
// Display a particular variable (in NAME=VALUE pair)
prompt> SET COMPUTERNAME
COMPUTERNAME=xxxxxx
Try issuing a SET command on your system, and study the environment variables listed. Pay
particular attention to the variable called PATH.
SET/Change/Unset a Variable: To set (or change) a variable, use "SET varname=value".
There shall be no spaces before and after the '=' sign. To unset an environment variable, use
"SET varname=", i.e., set to an empty string.
> set varname
Display the value of the variable
> set varname=value Set or change the value of the variable Delete the variable by
> set varname=
setting to empty string (Note: nothing after '=')
> set
Display ALL the environment variables
For examples,
// Set an environment variable
prompt> SET MYVAR=hello
// Display
prompt> SET MYVAR
MYVAR=hello
// Unset an environment variable
prompt> SET MYVAR=
// Display
prompt> SET MYVAR
Environment variable MYVAR not defined
A variable set via the "SET" command under CMD is a local variable, available to that CMD
session only.
Set an Environment Variable: To set an environment variable permanently in Windows, you
need to use the "Control Panel" ⇒ "System" ⇒ (Vista/7) "Advanced system settings" ⇒ Switch to
"Advanced" tab ⇒ "Environment variables" ⇒ Choose "System Variables" (for all users) or "User
Variables" (for this login user only) ⇒ choose "Edit" (for modifying an existing variable) or "New"
(to create a new variable) ⇒ Enter the variable "Name" and "Value".
Use a Variable: To reference a variable, use %varname% (with prefix and suffix of '%'). For
example, you can use the ECHO command to print the value of a variable in the form "ECHO
%varname%".
// Display the PATH environment variable. Same as SET PATH
prompt> ECHO %PATH%
PATH=xxxxxxx
// Append a directory in front of the existing PATH
prompt> SET PATH=d:\bin;%PATH%
PATH=d:\bin;xxxxxxx
For Unix Users
In Unixes:
 For bash: Use "env" to list all the environment variable and "set" to list all the variables.
Use "varname=value" to set a local variable. Use "export varname" to export this
variable to the environment and make the variable available to other processes.
 For csh, ksh: Use "printenv" (or "env") to list all the environment variables. Use "setenv
varname value" and "unsetenv varname" to set and unset an environment variable.
Use "set varname=value" and "unset varname" to set and unset a (local) variable for
the current process.
In Unixes, a variable is referenced as $varname, with a prefix '$'.
Java Applications and the Environment Variables
Many problems in the installation and running of Java applications are caused by incorrect
setting of environment variables (global system variables available to all the applications running
under the system), in particular, PATH, CLASSPATH, JAVA_HOME.
PATH and CLASSPATH
When you launch a program from the command line, the operating system uses the PATH
environment variable to search for the program in your local file system. In other words, PATH
maintains a list of directories for searching executable programs.
If your Java application uses other Java classes, the JDK/JRE uses the CLASSPATH environment
variable to search for the classes in your local file system. In other words, CLASSPATH maintains a
list of directories for searching Java classes referenced in your Java application. For ease of
distribution, Java classes are often zipped together into a so-called JAR-file. CLASSPATH could
include directories and JAR files.
PATH is applicable to all applications; while CLASSPATH is used by Java applications only.
PATH (For Windows Users)
When you launch an executable program (with file extension of " .exe", ".bat" or ".com") from
the CMD shell, Windows searches the program in the current working directory and all the
directories listed in the PATH. If the program cannot be found, you will get the following error:
xxxx is not recognized as an internal or
"cmd.exe" on the newer Windows
external command, operable program or batch
systems (2000/XP/Vista/7)
file.
Bad command or file name
"command.com" on the older Windows
systems (95/98)
PATH maintains a set of directories. The directories are separated by semi-colon ';'.
For Java applications, PATH must include the following directories:
 JDK's "bin" directory (e.g., "c:\Program Files\java\jdk1.6.0_xx\bin"), which contains
JDK programs such as Java Compiler "javac.exe" and Java Runtime "java.exe".
 "c:\windows\system32" and "c:\windows" which contain console programs and
commands.
NOTES: The JDK's "bin" directory should be listed before "c:\windows\system32" and
"c:\windows" in the PATH. This is because some older Windows systems provide their own Java
runtime (which is often outdated) in these directories (try search for " java.exe" in your
computer!).
To manipulate the PATH environment variable, you could use command "set PATH" (just like any
environment variable). But as PATH is frequently used, a dedicated command called path is
provided.
prompt> PATH
Display all the search paths, same as SET PATH and ECHO %PATH%
prompt> PATH=value
Set the value of PATH, same as SET PATH=value (note: no space
prompt> PATH=
before and after '=')
Delete PATH, same as SET PATH=
prompt>PATH=D:\bin;D:\bin\java Set search paths, separated by semi-colon
prompt> PATH=D:\bin;%PATH%
Insert "D:\bin" in front of the current PATH
In Windows, the current working directory '.' is automatically included in the PATH, as the first
entry. In other words, the current working directory is searched first, before searching the other
paths specified in PATH, in a the order specified.
For Windows users, you should set the PATH permanently to include JDK's "bin" directory via
"Control Panel" ⇒ "System"⇒ (Vista/7) "Advanced system settings" ⇒ Switch to "Advanced" tab
⇒ "Environment variables" ⇒ Choose "System Variables" (for all users) or "User Variables" (for
this login user only) ⇒ Select variable "PATH" ⇒ Choose "Edit" (for modifying an existing variable)
⇒
In
variable
"Value",
APPEND
your
JDK's
"bin"
directory
(e.g.,
"c:\Program
Files\java\jdk1.6.0_xx\bin"), followed by a semi-colon ';', IN FRONT of all the existing PATH
entries. DO NOT remove any entry; otherwise, some programs may not run.
CLASSPATH (For Windows User)
Suppose that your Java application uses other Java classes. If the Java class cannot be found in
the Java’s default core package (java.lang), import classes, or one of the directories or jar-files
listed in CLASSPATH, you will receive an error:
Exception in thread "main" java.lang.NoClassDefFoundError: xxxx
CLASSPATH accepts directories and jar-files. The directories and jar-files are separated by semi-
colon ';'. An example of CLASSPATH setting, which includes the current working directory
(denoted
as
dot
'.')
and
Java
Servlets
API
d:\tomcat\lib\servlet.jar) is as follow:
prompt> SET CLASSPATH=.;d:\tomcat\lib\servlet.jar
jar-file
from
Tomcat
(e.g.,
Since JDK 1.3, if no CLASSPATH is set explicitly, the default is set to the current working directory
'.'. However, if you explicitly set your CLASSPATH, you have to include the current directory '.'
explicitly. Otherwise, the current directory will not be searched. A common problem during JDK
installation is CLASSPATH was set (probably by another application) without including the current
directory. You will receive "NoClassDefFoundError" when you run your Hello-world program.
For a beginner, no explicit CLASSPATH setting is required. The default CLASSPATH setting of
currently directory is sufficient. Remove all CLASSPATH setting if there is any. However, if you have
to set CLASSPATH, make sure that you include the current directory '.'.
To temporarily changing CLASSPATH for this CMD session:
// Display current setting of CLASSPATH
prompt> SET CLASSPATH
// Unset (remove) CLASSPATH
prompt> SET CLASSPATH=
// Set CLASSPATH to the current directory '.'
prompt> SET CLASSPATH=.
// Set CLASSPATH to the current directory and a JAR file
prompt> SET CLASSPATH=.;d:\tomcat\lib\servlet-api.jar
For windows users, you can set the CLASSPATH permanently via "Control Panel" ⇒ "System"⇒
(Vista/7) "Advanced system settings" ⇒ Switch to "Advanced" tab ⇒ "Environment variables" ⇒
Choose "System Variables" (for all users) or "User Variables" (for this login user only):
 (RECOMMENDED) Delete CLASSPATH from both System and User variables.
 To modify the existing CLASSPATH, select variable "CLASSPATH" and Choose "Edit" ⇒ In
variable "Value", provide the directories and jar-files, separated by semi-colon ';'.
Make sure that the current directory '.' is included as the first entry.
 To create CLASSPATH ⇒ Choose "New" ⇒ In variable "Name", enter "CLASSPATH" ⇒ In
variable "Value", provide the directories and jar-files, separated by semi-colon ';'.
Make sure that the current directory '.' is included as the first entry.
Read JDK documents "Setting the CLASSPATH" and "How Classes are Found" (you can find the
hyperlinks from the index page of the JDK documentation, or googling).
JAVA_HOME and JRE_HOME
JRE (Java Runtime) is needed for running Java programs. JDK (Java Development Kit) is needed
for writing and running Java programs. JDK includes JRE plus development tools such as Java
compiler.
Set JAVA_HOME to your JDK installation directory (e.g., "c:\Program Files\java\jdk1.6.0_xx").
JAVA_HOME is needed for running Tomcat and many other Java applications.
You
can
optionally
set
JRE_HOME
to
the
JRE
base
directory
(e.g.,
"c:\Program
Files\java\jre1.6.0_xx")
For Unix Users
The above discussion for PATH, CLASSPATH, JAVA_HOME and JRE_HOME applied, except that:
 Unix uses ':' as the path separator, and forward slash '\' as directory separator.
 The current working directory is not searched, unless it is included in the PATH. You can
denote current working directory as '.' in the PATH.
 The program shall have executable file attribute.
 If you invoke a program from the command-line and it cannot be located in the PATH,
you get an error message "command not found".
 You can set the environment variables (PATH, CLASSPATH) permanently in your login or
shell initialization script, such as .login, .cshrc, .bashrc.
(For Advanced Users) Windows vs. Unixes
Java is platform independent. Java classes run in Windows as well as Unixes - binary compatible.
 Unixes have many shells, such as the newer bash and the older csh, ksh. Windows have
two shells: the newer cmd.exe and the older command.com. Each shell come with its own
set of commands, utilities, and its own scripting programming language.
 Unix's variable name is denoted as $varname, e.g., $JAVA_HOME. Windows uses %varname%,
e,g., %JAVA_HOME%.
 Unix uses command "printenv" (print environment) or "env" to list all the environment
variables. Windows uses command "set".
 Unix's PATH is set permanently in the login or shell initialization script (e.g., ".login" or
".bashrc"). Windows' PATH is set in Control Panel ⇒ System.
 The current directory is NOT included in the Unix's PATH implicitly. You need to include
"." in the PATH environment variable explicitly. On the other hand, current directory is
included in Windows' PATH implicitly.
 A Windows' path includes a drive letter and directories. It uses back-slash '\' as directory
separator (e.g., "c:\jdk1.6\bin"). Linux's paths do not have drive letter and use
forward slash '/' as the directory separator (e.g., "/usr/bin/jdk1.6").
 Windows use semi-colon ';' as path separator, while Unixes use colon ':'.
 Windows/DOS uses "0D0AH" (carriage-return plus line-feed) as line-break (or End-of-Line
(EOL)). Unix uses "0AH" (line-feed) only.
(Further details can be found at;
http://www.ntu.edu.sg/home/ehchua/programming/howto/Environment_Variables.html)