2 - Installing JDK
2 - Installing JDK
Java Development Kit (JDK) 1.8 (officially named Java SE 8), freely available from Sun
Microsystems (now part of Oracle), is needed for writing Java programs. JDK can be
downloaded from the Java mother site @ http://java.sun.com (or
http://www.oracle.com/technetwork/java/index.html).
Choose your operating platform, e.g., Windows x86 (for 32-bit Windows) "jdk-8-windows-
i586.exe"
STEP 2: Install JDK/JRE - Run the downloaded installer, which installs both the JDK (Java
Development Kit) and JRE (Java Runtime). By default, the JDK and JRE will be installed
into directories "C:\Program Files\java\jdk1.8.0" and "C:\Program Files\java\jre8",
respectively. For novices, accept the defaults.
(For Advanced Users Only) The default JDK/JRE directories work but I recommend
avoiding "Program Files" because of that blank character in the directory name which is not
Unix-friendly. You may change the installed directory for JDK and JRE during installation. I
personally installed JDK and all my programming tools in "C:\bin" (instead of "C:\Program
Files") for ease of maintenance. Furthermore, it is always cleaner to un-install all the out-
dated JDK/JRE before installing a new version.
STEP 3: Include JDK's "bin" directory in the PATH - Windows Operating System
searches the current directory and the directories listed in the PATH environment variable for
executable programs invoked from the CMD shell. JDK's programs (such as compiler
javac.exe and runtime java.exe) reside in directory "$JAVA_HOME\bin" (where
$JAVA_HOME denotes the JDK installed directory you have chosen in the previous step).
The "bin" directory needs to be included in the PATH environment variable.
Click the "Start" button ⇒ "Control Panel" ⇒ "System" ⇒ (Vista/7 only) "Advanced system
settings".
In "Variable value" field, insert "c:\Program Files\java\jdk1.8.0\bin" (assume that this is your
JDK's binary directory) in front of all the existing directories, followed by a semi-colon (;) to
separate the JDK's binary directory from the rest of the existing directories. DO NOT delete
any existing entries; otherwise, some existing applications may not run.
(For Advanced Users Only) I suggested that you place the JDK binary directory in front of
"c:\windows\system32" and "c:\windows". This is because some Windows systems may have
an out-dated copy of JDK/JRE in these directories. Do a search for "java.exe", and you will
be amazed of the findings.
You could read "Java Applications and Environment Variable" for more discussions about
PATH environment variable.
STEP 4: Verify the JDK Installation - Launch a CMD shell (click the "Start" button ⇒
run... ⇒ enter "cmd"), and
Issue a "PATH" command to list the content of the PATH environment variable (i.e., search
paths for executable programs). Check the output and make sure that $JAVA_HOME\bin is
listed in the PATH.
prompt> path
PATH=c:\Program Files\java\jdk1.8.0\bin;..........
Issue the following commands to check that JDK is properly installed and display its version:
prompt> javac
.........
.........
Read "Writing your First Java Program with JDK and a Programming Text Editor".
STEP 6: Download JDK API Documentation - The JDK download does not include the
documentation, which needs to be downloaded separately. In the past, I always insist my
students to download a local copy of JDK API Documentation. But, today, you can easily
access the online copy by googling "JDK 7 Documentation".
Unzip into the $JAVA_HOME (JDK installed directory). The documentation will be
unzipped into "$JAVA_HOME\docs". Browse the JDK documentation by opening
"$JAVA_HOME\docs\index.html".
STEP 7: (For Advanced Users Only) JDK's Source Code - Source code for JDK is
provided and kept in "$JAVA_HOME\src.zip". I strongly recommend that you to go through
some of the source files such as "String.java", "Math.java", and "Integer.java", under
"java\lang".
Symptom: Cannot compile Java program (e.g., javac Hello.java) from the
CMD shell
ERROR MESSAGE
PROBABLE CAUSES
SOLUTION
First, start a CMD shell (click "Start" button ⇒ "run..." ⇒ enter "cmd") and issue a PATH
command:
prompt> path
PATH=.......
Check if it includes your JDK's bin directory. For example, suppose that your JDK is
installed in "c:\program files\java\jdk1.8.0", then PATH should include "c:\program
files\java\jdk1.8.0\bin".
Otherwise, include JDK's bin directory in the PATH environment variable. Read Step 3 of
"How to install JDK".
Symptom: Can compile but cannot run Java program (e.g., java Hello)
from the CMD shell
ERROR MESSAGE
PROBABLE CAUSES
The CLASSPATH environment variable is set, but does not include the current directory ".".
If the class file is present in the current directory, issue a "set CLASSPATH" command to
display its settings:
CLASSPATH=.......
If you receive the message "Environment variable CLASSPATH not defined" and your
program is correct, I can't help you here.
Otherwise, if the CLASSPATH is defined, for beginner, I suggest that you remove the
CLASSPATH environment variable. From "Control Panel" ⇒ System ⇒ (Vista only)
Advanced system settings ⇒ Switch to "Advanced" tab ⇒ Environment Variables ⇒ System
variables (and also User variables) ⇒ Select variable "CLASSPATH" ⇒ Delete. (Delete from
both the System variables and User variables.)
(For Advanced Users Only) If CLASSPATH is not set, it is defaulted to the current
directory. However, if CLASSPATH is set, the current directory is NOT implicitly included.
You can include the current directory (denoted by a single dot ".") in front of the existing
class-paths. Read "Java Applications and Environment Variable" for more discussion on
CLASSPATH.
ERROR MESSAGE
PROBABLE CAUSES
Your program does not have a main() method (check the SPELLING carefully).
The signature of the main() method is incorrect (public static void main(String[]
args) { ... }).
SOLUTION
Check that your program including a main() method with the correct signature including an
argument of "String[] args" (check the SPELLING carefully):
External Java packages (such as JOGL, JUnit) are often distributed in JAR files with possibly
native libraries (lib, dll). To use an external package in JDK, you need to:
include the native libraries's directory in the PATH. (To be precise: The native libraries are to
be kept in a directory accessible via JRE's property "java.library.path", which normally but
not necessarily includes all the paths from the PATH environment variable).
If you cannot compile your program, make sure that the all the JAR files are included in the
CLASSPATH. (Native libraries are not involved in the compilation.)
You could set the native library path via the command-line option -
Djava.library.path=xxx.
Alternatively, you could copy the JAR files into JDK's extension directory at
"$JDK_HOME\jre\lib\ext"; and copy all the dll's and lib's into a directory included in
java.library.path.
Use the JDK and your favorite text editor. Compile and interpret the program in
a shell window.
Use the JDK and a text editor that is integrated with JDK for example Emacs,
Texpad, IntelliJ, and JEdit. Compile and interpret programs inside the editor.
First choice
(Use the JDK and your favorite text editor. Compile and interpret the program
in a shell window.)
then before you can run your programs change the directory to the one where your programs
are
: cd \ java
Confirm that your machine has the Sun Java Development Kit Installed in the
directory
\jdk1.8
\jdk1.8\bin\javac
\jdk1.8\bin\java
Open file HelloWorld.java using the default DOS editor “edit” and enter the
following java source code.
javac HelloWorld.java
If your machine cannot find the command “javac” then either enter the path to
this file as:
\jdk1.8\bin\javac HelloWorld.java
PATH=%PATH%;jdk1.8\bin
If necessary enter this command into the \aoutoexec.bat file of your machine.
Close all programs running in the background and foreground and reboot the
machine using CTRL+ALT+DEL. After logging on again confirm that the path
is correct using:
PATH
If the path now contains two semicolons in succession (;;) then reedit the
autoexec.bat file and delete the semicolon in the line previously entered and
reboot the machine, log on again and confirm that the path is correct. Change to
your CT216 directory and resume compilation of your Java program.
If the compiler complains about syntax errors, attend to these. If the compiler
does not report any errors confirm that your current directory contains the
compiled Java bytecode in file:
HelloWorld.class
Inspect this file using “edit” and satisfy yourself that it is not Java source code.
java HelloWorld
and confirm that the expected results are obtained. If not, correct your code and
recompile and rerun until you obtain the expected results.
javadoc HelloWorld.java
confirm that HelloWorld.html, index.html and other HTML files are generated.
Minimize the DOS window. Confirm that your system includes the TextPad
editor in:
\Program Files\TextPad
If not download, the compressed archive TextPad.zip from the internet, unzip it
in a scratch directory on your machine, run the setup.exe program to install it
into the
Open your HelloWorld.java file using TextPad. Use the tools, Java Compile,
menu to compile your program, and the Tools, Java Run, menu to run your
program.
If your copy of TextPad does not include these menu items, create them as
follows:
Use the configure, preferences, tools,menu, choose “new” and enter the file
name.
Change the “Menu Text” field to read “Java Compile”; enter “-deprecation
$File” in the “Parameters” field; enter “$FileDir” in the “Initial folder” field;
choose “OK” to save your changes.
Use the Configure, Preferences, Tools, menu; choose “new” and enter the file
name:
c\Program Files\TextPad\RUNJAVA.BAT
Change the “Menu Text” field to read “Java Run” enter “$BaseName” in the
“Parameters” field enter “$FileDir” in the “Initial Folder” field; choose “OK”
to save your changes.
Confirm that your system includes the Sun Java JDK documentation in
\jdk1.8\docs.
This online documentation is your primary Java Resource and you will need to
use it confidently. Open the file \jdk1.8\docsindex.html and see what you
can discover about the method “println()”