Java programs are compiled and run from an operating system prompt, unless you have installed an IDE that will do this for you directly. If you create an applet, this would run inside a web page in a browser.
After you have installed the JDK, you will need to set at least one environment variable in order to be able to compile and run Java programs.
For more complex projects that pull together elements from different sources, you must set an additional environment variable or two. Note that:
- a
PATH
environment variable enables the operating system to find the JDK executables when your working directory is not the JDK's binary directory. CLASSPATH
is Java's analog toPATH
, the compiler and JVM use it to locate Java classes.- Often you will not need to set this, since the default setting is to use the JDK's library jar file and the current working directory.
- But, if you have additional Java classes located in another directory (a third-party library, perhaps), you will need to create a classpath that includes not only that library, but the current working directory as well (the current directory is represented as a dot).
- Many IDE's and servers expect to find a
JAVA_HOME
environment variable.- This would be the JDK directory (the one that contains
bin
andlib
). - The
PATH
is then set fromJAVA_HOME
plus\bin
. - This makes it easy to upgrade your JDK, since there is only one entry you will need to change.
- This would be the JDK directory (the one that contains
Best Practice: Setting PATH
from JAVA_HOME
The procedure to permanently set the environment variables varies slightly from one version of Windows to another; the following will work in many, including Windows XP. The process for Vista is similar, but slightly different:
- Right-click on My Computer.
- Choose Properties.
- Select the Advanced tab.
- Click the Environment Variables button at the bottom.
- Check both the User and System variable lists to see if
JAVA_HOME
orPATH
already exist. - If
JAVA_HOME
exists, check to see that it matches your most recent JDK (or the one you wish to use).- It is probably better to set this as a System variable.
- If it exists, click Edit, if not, click Add.
- For the variable name, enter
JAVA_HOME
. - For the value, enter your JDK directory, such as
C:\Program Files\Java\jdk1.5.0_14
.- Note that the space in the name can sometimes cause problems.
- One solution is to put quote marks around the entry, as in
"C:\Program Files\Java\jdk1.5.0_14"
. - An even better solution would be to use the 8-character form of the directory name, such as
C:\Progra~1\Java\jdk1.5.0_14
(you can check if this works in your system by typing it into the address bar of a My Computer window).
- for
PATH
, again select either Add or Edit.- You could do this either as a User variable or a System variable.
- If there isn't already a JDK bin directory mentioned in the
PATH
, it will work as a User variable. - If there is already a JDK mentioned, you would want to ensure that this new entry preceded the existing entry, so you would edit that variable.
- Note that if the existing entry was created using
JAVA_HOME
, then we are already set correctly . - If you "prepend" an entry to
PATH
, it will be found first, and therefore supercede any other directory - if you append to path, your directory won't be found if an earlier entry JDK entry exists (the following image shows a prepend). - Also note that System variables precede User variables, so they will be found first.
Setting Environment Variables from a Command Prompt
If you set the variables from a command prompt, they will only hold for that session, but you could create a batch file that you could run each time you open a command prompt window.
To set the
PATH
from a command prompt or batch file:
1
| set PATH=C:\Progra~ 1 \Java\jdk1. 6 .0_10\bin;%PATH% |
If you need to set the
CLASSPATH
:
1
| set CLASSPATH=.;%CLASSPATH% |
- Early versions of the JDK required you to include the JDK's lib directory in the
CLASSPATH
; this is no longer necessary . - Note that UNIX environments use
$PATH
and$CLASSPATH
instead of%PATH%
and%CLASSPATH%
, and that the path element separator is a colon instead of a semicolon.
0 comments :
Post a Comment