0% found this document useful (0 votes)
69 views

2 - Installing JDK

The document provides instructions for installing the Java Development Kit (JDK) and setting it up on Windows. It involves 7 steps: 1) Downloading the JDK, 2) Installing the JDK, 3) Adding the JDK bin directory to the system PATH, 4) Verifying the installation, 5) Compiling and running a sample Java program, 6) Downloading JDK documentation, and 7) Accessing JDK source code. It also describes common installation errors and their solutions.

Uploaded by

Tanaka Maj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

2 - Installing JDK

The document provides instructions for installing the Java Development Kit (JDK) and setting it up on Windows. It involves 7 steps: 1) Downloading the JDK, 2) Installing the JDK, 3) Adding the JDK bin directory to the system PATH, 4) Verifying the installation, 5) Compiling and running a sample Java program, 6) Downloading JDK documentation, and 7) Accessing JDK source code. It also describes common installation errors and their solutions.

Uploaded by

Tanaka Maj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

How to Install and Get Started

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).

STEP 1: Download JDK

Goto Java SE download site @


http://www.oracle.com/technetwork/java/javase/downloads/index.html.

Select "Java SE 8", "JDK".

Choose your operating platform, e.g., Windows x86 (for 32-bit Windows) "jdk-8-windows-
i586.exe"

(For Mac Users Only) Download JDK from http://developer.apple.com/java/download/ and


follow the instructions to install (or google "how to install JDK on Mac").

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.

The JDK installed directory is hereafter denoted as $JAVA_HOME (Unix notation) or


%JAVA_HOME% (Windows notation) in this writing.

(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.

To edit the PATH environment variable in Windows 2000/XP/Vista/7:

Click the "Start" button ⇒ "Control Panel" ⇒ "System" ⇒ (Vista/7 only) "Advanced system
settings".

Prepared By Zanamwe N @ 2014


Switch to "Advanced" tab ⇒ "Environment Variables..."

In "System Variables" box, scroll down to select "PATH" ⇒ "Edit..."

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.

Variable name : PATH

Variable value : c:\Program Files\java\jdk1.8.0\bin;....exiting entries....

(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> java -version

java version "1.8.0"


Java(TM) SE Runtime Environment (build 1.8.0-b147)
Java HotSpot(TM) Client VM (build 21.0-b17, mixed mode, sharing)

prompt> javac

Usage: javac <options> <source files>

.........

.........

STEP 5: Compile & Run a Java Hello-world Program

Read "Writing your First Java Program with JDK and a Programming Text Editor".

Prepared By Zanamwe N @ 2014


Read "Common JDK Installation Errors", if you encounter problems compiling or running
your first Java program.

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".

To install JDK API documentation:

From the Java SE download page (@


http://www.oracle.com/technetwork/java/javase/downloads/index.html), look for "Java SE 8
Documentation" (under "Additional Resources") ⇒ Download the zip-file (e.g., "jdk-
8....zip").

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".

Common JDK Installation Problems

Symptom: Cannot compile Java program (e.g., javac Hello.java) from the
CMD shell

ERROR MESSAGE

'javac' is not recognized as an internal or external command, operable program or


batch file.

PROBABLE CAUSES

Prepared By Zanamwe N @ 2014


The PATH environment variable, which maintains a list of search paths for executable
programs, including "javac.exe", does not include JDK's bin directory.

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

Exception in thread "main" java.lang.NoClassDefFoundError: Hello

PROBABLE CAUSES

The class (e.g., Hello.class) is NOT in the current directory.

The CLASSPATH environment variable is set, but does not include the current directory ".".

Prepared By Zanamwe N @ 2014


SOLUTION
First, issue a "dir" command to list the contents of the current directory. Check that it
contains the class to be run (e.g., Hello.class). You need to compile the source program to get
the class file.

If the class file is present in the current directory, issue a "set CLASSPATH" command to
display its settings:

prompt> set classpath

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

Exception in thread "main" java.lang.NoSuchMethodError: main

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):

public static void main(String[] args) { ...... }

Prepared By Zanamwe N @ 2014


(For Advanced Users Only) Using External JAR files and Native Libraries
In JDK

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 ALL the jar-files in the CLASSPATH (e.g.,


classpath=.;path1\a.jar;path2\b.jar).

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.)

If you get a runtime error "java.lang.UnsatisfiedLinkError: no xxx in java.library.path". This


means that JRE cannot find your native library at runtime. You could print the contents of
"java.library.path" via
System.out.println(System.getProperty("java.library.path")) and check
whether it includes your native library's path.

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.

Choices for development environment

Use the JDK and your favorite text editor. Compile and interpret the program in
a shell window.

Use integrated development environment such as eclipse


(http://eclipse.org).

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.

Prepared By Zanamwe N @ 2014


Use Netbeans which is an integrated environment

First choice

(Use the JDK and your favorite text editor. Compile and interpret the program
in a shell window.)

After installing JDK you need to create a directory:

md\ java // put all your programs here

then before you can run your programs change the directory to the one where your programs
are

: cd \ java

Compile your programs using: javac programname.java

Execute your programs by: java programname

IS 222 Practical Exercise 01

Confirm that your machine has the Sun Java Development Kit Installed in the
directory

\jdk1.8

Including the Java compiler and the Java Interpreter

\jdk1.8\bin\javac

\jdk1.8\bin\java

Prepared By Zanamwe N @ 2014


If not, download the self extracting archive javasdk2.exe from Oracle Website
and run it in the root (\) directory on your machine. In your home directory
create directory java . In directory java create directory Prac-01. Change
directory to Prac-01

Open a DOS window

Open file HelloWorld.java using the default DOS editor “edit” and enter the
following java source code.

/** Program to print "Hello world" to the console screen */

public class HelloWorld

{ public static void main(String[] args)

{ System.out.println("Hie Java! How are you");

Compile HelloWorld.java using the Java compiler

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

Prepared By Zanamwe N @ 2014


Or better, enter this path into your PATH using:

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.

Run this file using the Java interpreter:

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.

Prepared By Zanamwe N @ 2014


Compile standard Java documentation for your program using the javadoc
utility:

javadoc HelloWorld.java

confirm that HelloWorld.html, index.html and other HTML files are generated.

Open index.html using your browser and explore it.

(note that here follows the second choice)

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

\Program Files\TextPad directory and delete the scratch directory to


recover the disk space.

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.

Prepared By Zanamwe N @ 2014


C:\jdk1.8\bin\javac.exe

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()”

Prepared By Zanamwe N @ 2014

You might also like