How To Install JDK 15 and Notepad (On Windows) and Get Started With Java Programming

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

How to Install JDK 15/

Notepad++ (Windows)
and Get Started with
Java Programming
Object Oriented Programming EE- 57

[Type the abstract of the document here. The abstract is


typically a short summary of the contents of the document.
Type the abstract of the document here. The abstract is
typically a short summary of the contents of the document.]

Lt Col M Imran Javaid


3/5/2021
2

How to Install JDK 15 (Windows)


The Java Development Kit (JDK), officially named "Java Platform Standard Edition" or "Java
SE", is needed for writing and running Java programs.

Currently, the OpenJDK developed by Oracle and the Java community (@


https://openjdk.java.net/) provides a free and open-source JDK official reference implementation.

This article is based on the Oracle JDK (@ https://www.oracle.com/java/) (due to legacy), which
is free for personal and development use but no longer free for commercial use.

JDK Versions

Reference: "Java Version History" @ https://en.wikipedia.org/wiki/Java_version_history.

1. JDK Alpha and Beta (1995): Sun Microsystem announced Java in September 23, 1995.
2. JDK 1.0 (January 1996): Originally called Oak (named after the oak tree outside James Gosling's
office). Renamed to Java 1 in JDK 1.0.2.
3. JDK 1.1 (February 1997): Introduced AWT event model, inner class, JavaBean, JDBC, and RMI.
4. J2SE 1.2 (JDK 1.2) (December 1998): Re-branded as "Java 2" and renamed JDK to J2SE (Java 2
Standard Edition). Also released J2EE (Java 2 Enterprise Edition) and J2ME (Java 2 Micro Edition).
Included JFC (Java Foundation Classes - Swing, Accessibility API, Java 2D, Pluggable Look & Feel,
and Drag & Drop). Also introduced Collection Framework and JIT compiler.
5. J2SE 1.3 (JDK 1.3) (May 2000): Introduced Hotspot JVM.
6. J2SE 1.4 (JDK 1.4) (February 2002): Introduced assert statement, non-blocking IO (nio),
logging API, image IO, Java webstart, regular expression (regex) support.
7. J2SE 5.0 (JDK 5) (September 2004): Officially called 5.0 instead of 1.5 (by dropping the 1.).
Introduced generics, autoboxing/unboxing, annotation, enum, varargs, for-each loop, static
import. See "JDK 5 New Features".
8. Java SE 6 (JDK 6) (December 2006): Renamed J2SE to Java SE (Java Platform Standard Edition).
No new language features. See "JDK 6 New Features".
9. Java SE 7 (JDK 7) (July 2011): First version after Oracle purchased Sun Microsystem - aslo called
OracleJDK. Introduced Strings in switch statement, Binary integer literals, allowing underscores
in numeric literals, improved type inference for generic instance creation (or diamond operator
<>), Catching multiple exception types and rethrowing exceptions with improved type checking.
See "JDK 7 New Features".
10. Java SE 8 LTS (JDK 8) (March 2014): Included support for Lambda expressions, default and static
methods in interfaces, improved collection, and JavaScript runtime. Also integrated JavaFX
graphics subsystem. See "JDK 8 New Features".
11. Java SE 9 (JDK 9) (September 21, 2017): Introduced modularization of the JDK ( module) under
project Jigsaw, the Java Shell (jshell), and more. See "JDK 9 New Features".
12. Java SE 10 (18.3) (JDK 10) (March 2018): Introduced var for type inference local variable (similar
to JavaScript). Introduced time-based release versioning with two releases each year, in March
and September, denoted as YY.M. Removed native-header generation tool javah. See "JDK 10
New Features".

2 Object Oriented Programming | EE-57


3

13. Java SE 11 LTS (18.9) (JDK 11) (September 2018): Extended var to lambda expression.
Standardize HTTP client in java.net.http. Support TLS 1.3. Clean up the JDK and the
installation package (removed JavaFX, JavaEE, CORBA modules, deprecated Nashorn JavaScript
engine). OracleJDK is no longer free for commercial use, but OpenJDK is still free. See "JDK 11
New Features".
14. Java SE 12 (19.3) (JDK 12) (March 2019): Switch Expression (preview). See "JDK 12 New
Features".
15. Java SE 13 (19.9) (JDK 13) (September 2019): Switch Expression (preview), Multi-line Text Block
(preview). See "JDK 13 New Features".
16. Java SE 14 (20.3) (JDK 14) (March 2020): Records (preview)
17. Java SE 15 LTS (20.9) (JDK 15) (September 2020):

"JDK" or "JRE"?

JRE (Java Runtime), which include a Java Virtual Machine and core libraries, is needed for
running Java programs. JDK (Java Development Kit), which includes JRE plus the development
tools (such as compiler and debugger), is need for writing as well as running Java programs. In
other words, JRE is a subset of JDK. Since you are supposed to write Java Programs instead of
merely running Java programs, you should install JDK, which includes JRE.

How To Install JDK on Windows


Step 0: Un-Install Older Version(s) of JDK/JRE

I recommend that you install only the latest JDK. Although you can install multiple versions of
JDK/JRE concurrently, it is messy.

If you have previously installed older version(s) of JDK/JRE, un-install ALL of them. Goto
"Control Panel" ⇒ (optional) "Programs" ⇒ "Programs and Features" ⇒ Un-install ALL
programs begin with "Java", such as "Java SE Development Kit ...", "Java SE Runtime ...", "Java
X Update ...", and etc.

Step 1: Download JDK

1. Goto JDK (or Java SE) download site @ https://www.oracle.com/java/technologies/javase-


downloads.html.
2. Under "Oracle JDK", click "JDK Download".
3. Download the "Windows x64 Installer" (e.g., " jdk-15.0.{x}_windows-x64_bin.exe" -
about 159MB), where {x} is a fast changing update number.

Step 2: Install JDK

Run the downloaded installer. Accept the defaults and follow the screen instructions to complete
the installation. By default, JDK is installed in directory "C:\Program Files\Java\jdk-
15.0.{x}", where {x} denotes the update number.

3 Object Oriented Programming | EE-57


4

Launch "File Explorer". Navigate to "C:\Program Files\Java" to inspect the directories. Take
note of your JDK Installed Directory jdk-15.0.{x}, in particular, the update number {x}
which you will need in the next step.

I shall refer to the JDK Installed Directory as <JAVA_HOME>, hereafter, in this article
(corresponding to environment variable %JAVA_HOME% in Windows or $JAVA_HOME in
Unix/macOS).

Step 3: (SKIP for JDK 15) Include JDK's "bin" Directory in the PATH

Windows' Command Prompt (CMD) searches the current directory and the directories listed in the
PATH environment variable for executable programs.

JDK's programs (such as Java compiler "javac.exe" and Java runtime "java.exe") reside in
the sub-directory "bin" of the JDK installed directory. JDK's "bin" needs to be added into the
PATH.

Prior to JDK 15, you need to explicitly add JDK's "bin" into the PATH. Starting from JDK 15, the
installation process adds the directory "C:\Program Files\Common
Files\Oracle\Java\javapath" to the PATH. The "javapath" directory is a link to
"javapath_target_xxxxxx", which contains a copy of the following JDK programs:

 java.exe: Java Runtime


 javac.exe: Java Compiler
 javaw.exe: Java Runtime for Windows Console-less
 jshell.exe: Java Command-line Shell (since JDK 10) - a Read-Evaluate-Print Loop (REPL) which
evaluates declarations, statements, and expressions as they are entered and immediately shows
the results.

Link is used so that you can keep multiple copies (versions) of JDK.

To edit the PATH environment variable in Windows 10:

1. Launch "Control Panel" ⇒ (Optional) "System and Security" ⇒ "System" ⇒ Click "Advanced
system settings" on the left pane.
2. Switch to "Advanced" tab ⇒ Click "Environment Variables" button.
3. Under "System Variables" (the bottom pane), scroll down to select variable "Path" ⇒ Click
"Edit...".
4. For Newer Windows 10:
You shall see a TABLE listing all the existing PATH entries (if not, goto next step). Click "New" ⇒
Click "Browse" and navigate to your JDK's "bin" directory, i.e., "c:\Program
Files\Java\jdk-15.0.{x}\bin", where {x} is your installation update number ⇒ Select
"Move Up" to move this entry all the way to the TOP.
5. For Older Windows 10 (Time to change your computer!):
(CAUTION: Read this paragraph 3 times before doing this step! Don't push "Apply" or "OK"
until you are 101% sure. There is no UNDO!!!)

4 Object Oriented Programming | EE-57


5

(To be SAFE, copy the content of the "Variable value" to Notepad before changing it!!!)
In "Variable value" field, APPEND "c:\Program Files\Java\jdk-15.0.{x}\bin" (where
{x} is your installation update number) IN FRONT of all the existing directories, followed by a
semi-colon (;) to separate the JDK's bin 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\jdk-15.0.{x}\bin;[do not delete
exiting entries...]

You need to re-started CMD for the new environment settings to take effect.

Step 4: Verify the JDK Installation

Launch a CMD via one of the following means:

1. Click "Search" button ⇒ Type "cmd" ⇒ Choose "Command Prompt", or


2. Right-click "Start" button ⇒ run... ⇒ enter "cmd", or
3. Click "Start" button ⇒ Windows System ⇒ Command Prompt

Issue the following commands to verify your JDK installation:

1. (Skip for JDK 15) Issue "path" command to list the contents of the PATH environment variable.
Check to make sure that your JDK's "bin" is listed in the PATH.

path
PATH=c:\Program Files\Java\jdk-15.0.{x}\bin;other entries...

2. Issue the following commands to verify that JDK/JRE are properly installed and display their
version:

// Display the JDK version


javac -version
javac 15.0.2

// Display the JRE version


java -version
java version "15.0.2" 2021-01-19
Java(TM) SE Runtime Environment (build 15.0.2+7-27)
Java HotSpot(TM) 64-Bit Server VM (build 15.0.2+7-27, mixed mode,
sharing)

Step 5: Write a Hello-World Java Program

1. Documentation is available at https://docs.oracle.com/en/java/javase/15/index.html for your


refrence. USE IT.
2. Create a directory to keep your works, e.g., "d:\myProject" or "c:\myProject". Do NOT
save your works in "Desktop" or "Documents" as they are hard to locate. The directory name
shall not contain blank or special characters. Use meaningful but short name as it is easier to
type.

5 Object Oriented Programming | EE-57


6

3. Launch a programming text editor (such as TextPad, NotePad++, Sublime Text, Atom). Begin
with a new file and enter the following source code. Save the file as " Hello.java", under your
work directory (e.g., d:\myProject).

/*
* First Java program to say Hello
*/
public class Hello { // Save as "Hello.java" under "d:\myProject"
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}

Step 6: Compile and Run the Hello-World Java Program

To compile the source code "Hello.java":

1. Start a CMD Shell (Search ⇒ enter "cmd" ⇒ select "Command Prompt").


2. Set the Current Drive to the drive where you saved your source file " Hello.java".
If you use drive "c", skip this step.
Else if you use drive "d", enter "d:" as follow:

d:
D:\xxx>

3. Set the Current Working Directory to the directory that you saved your source file via the cd
(Change Directory) command. For example, suppose that your source file is saved in directory
"myProject".

cd \myProject
D:\myProject>

4. Issue a dir (List Directory) command to confirm that your source file is present in the current
directory.

dir
......
xx-xxx-xx xx:xx PM 277 Hello.java
......

5. Invoke the JDK compiler "javac" to compile the source code " Hello.java".

javac Hello.java
// If error message appears, correct your source code and re-compile

The compilation is successful if the command prompt returns. Otherwise, error messages would
be shown. Correct the errors in your source file and re-compile. Check "Common JDK Installation
Errors", if you encounter problem compiling your program.

6 Object Oriented Programming | EE-57


7

6. The output of the compilation is a Java class called " Hello.class". Issue a dir (List Directory)
command again to check for the output.

dir
......
xx-xxx-xx xx:xx PM 416 Hello.class
xx-xxx-xx xx:xx PM 277 Hello.java
......

To run the program, invoke the Java Runtime " java":

java Hello
Hello, world!

Everything that can possibly go wrong will go wrong: Read "JDK Installation Common Errors".

Step 7: (For Advanced Users Only) JDK's Source Code

Source code for JDK is provided and kept in "<JAVA_HOME>\lib\src.zip" (or


"<JAVA_HOME>\src.zip" prior to JDK 9). I strongly recommend that you to go through some of
the source files such as "String.java", "Math.java", and "Integer.java", under
"src\java.base\java\lang", to learn how experts program.

7 Object Oriented Programming | EE-57


8

HOW TO DEAL WITH ERROR MESSAGES.

Murphy's Law states that:

 "Anything that can possibly go wrong, does."


 "Everything that can possibly go wrong will go wrong."
 "If anything can go wrong, it will."
 "If there is any way to do it wrong, he will."

When software goes wrong, the MOST IMPORTANT thing to do is to FIND the ERROR
MESSAGE, which can give you clues of what went wrong. If things were running fine until the
lightning strikes, ask yourself what have you CHANGED!

Search google with your error message.

How to find error message.

Stack Trace. Most of the times, the error message consists of tens of lines of so-called stack
trace of method invocation. That is, method A called method B, which called method C, and so
on, until method Z encountered an error and threw an Exception or an Error. It is important to:

1. Get to the first line of the error message to read the description, and
2. Look for the line number of YOUR PROGEAM that triggered the error.

For example, this error message (stack trace) has 40 over lines:

8 Object Oriented Programming | EE-57


9

9 Object Oriented Programming | EE-57


10

How to Install Notepad ++ (Windows)


Notepad++ (The COMPSCI 101 Text Editor for Windows)

The text editor that we will be using in the Class for creating our Java
programs is called Notepad++ and is freely available for the Windows
operating system. Freely available at http://notepad-plus-plus.org .This document
describes how to install and configure Notepad++ using the settings to run java.

How To Install JDK on Windows

Step 1: Download and install Notepad ++

10 Object Oriented Programming | EE-57


11

Step 2: Configure Notepad ++

11 Object Oriented Programming | EE-57


12

12 Object Oriented Programming | EE-57


13

Short-‐cut keys

Define the function and the corresponding short-cut keys for running Java
programs:

1. Go to Plugins > NppExec > Execute...

2. Choose "Cancel"if a Save File... dialog box appears–probably will not


happen

3. Enter the following command in the window as shown below

13 Object Oriented Programming | EE-57


14

Then choose Save... and,using the name "Run Java", save this script (see
below):

Press OK

14 Object Oriented Programming | EE-57


15

15 Object Oriented Programming | EE-57


16

Now select "Run Java" from under the "Associated script:"menu, and click
the “Add/Modify" button.

You should see the "Run Java" script added in the "Menu
items" box as shown below:

16 Object Oriented Programming | EE-57


17

17 Object Oriented Programming | EE-57


18

18 Object Oriented Programming | EE-57


19

JAVA

Note that you may first have to remove Ctrl 1 shortcut from one of the commands
in the Main menu list of commands (the list on the left most menu).
19 Object Oriented Programming | EE-57
20

Double-‐click the "Run Java" script name to bring up the "Shortcut" box. In this
box, you can enter the desired short cut for the "Run Java" script. Select CTRL+1 as
shown below:

RUN JAVA

and then click the "OK" button and close the window.

Testing The Script

In Java, every application begins with a class name, and that class must
match the filename.

Let's create our first Java file, called Main.java, which can be done in any
text editor (like Notepad).

The file should contain a "Hello World" message, which is written with the
following code:

public class Main {


public static void main(String[] args) {
System.out.println("Hello World");
}
}

Save the code in Notepad++ as "Main.java" in some location on


your disk (remember the file should have a .java extension)

20 Object Oriented Programming | EE-57


21

You should now be able to use the short-‐cut keys(or you can select them from
the "Macro" menu) to run your program:

RUN JAVA

SUCCESS

21 Object Oriented Programming | EE-57


22

TORUBLESHOOTING NOTEPAD+++

Sometimes things don't work as planned. Two things which may not work
when you install Notepad++ are

1. Short-‐cut keys. Please note that the script for running a Java program is:

cd "$(CURRENT_DIRECTORY)"

C:\Program Files\Java\jdk-15.0.2\bin\javac "$(FILE_NAME)"

C:\Program Files\Java\jdk-15.0.2\bin\java "$(FILE_NAME)"

" C:\Program Files\Java\jdk-15.0.2\bin\" is the path to the place in your system


where javac.exe and java.exe exists. If your program doesn't run then check that
this path is correct. How to check (see below).Go to the folder in the C drive
where you rfolder containing the Java files are stored:

Open this folder and you will find your java and javac executable:

22 Object Oriented Programming | EE-57


23

Note that sometimes the executable starts with lower case 'j') If the path to your
java.exe or javac.exe is different to the one shown in these notes, then change
the script accordingly:

cd "$(CURRENT_DIRECTORY)"

C:\WHATEVER_YOUR_PATH\bin\javac "$(FILE_NAME)"

C:\ WHATEVER_YOUR_PATH \bin\java "$(FILE_NAME)"

2. NppExec Plug-in. Please note that the script for running a Java program is:

23 Object Oriented Programming | EE-57


24

24 Object Oriented Programming | EE-57

You might also like