How To Install JDK 11 (On Windows, Mac OS & Ubuntu) and Get Started With Java Programming
How To Install JDK 11 (On Windows, Mac OS & Ubuntu) and Get Started With Java Programming
How To Install JDK 11 (On Windows, Mac OS & Ubuntu) and Get Started With Java Programming
3. Issue the following commands to verify that JDK/JRE are properly installed and
display their version:
D:\xxx>
4. 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".
5. cd \myProject
D:\myProject>
6. Issue a dir (List Directory) command to confirm that your source file is present in
the current directory.
7. dir
8. ......
9. xx-xxx-xx xx:xx PM 277 Hello.java
......
10. Invoke the JDK compiler "javac" to compile the source code "Hello.java".
javac Hello.java
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.
11. The output of the compilation is a Java class called "Hello.class". Issue a dir (List
Directory) command again to check for the output.
12. dir
13. ......
14. xx-xxx-xx xx:xx PM 416 Hello.class
15. xx-xxx-xx xx:xx PM 277 Hello.java
......
To run the program, invoke the Java Runtime "java":
java Hello
Hello, world!
You should also download the "JDK x Samples and Demos", and study these samples.
Step 8: (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
"java\lang".
1.1 Using TextPad for Java Programming (on Windows)
From the TextPad editor, you can invoke the JDK compiler/runtime directly via "Tools"
menu ⇒ External Tools ⇒ "Compile Java" or "Run Java Application". Take note of the
keyboard shortcuts - Ctrl+1 for compile and Ctrl+2 for run.
If you cannot find these commands in the "Tools" menu, goto "Configure" ⇒
Preferences... ⇒ Tools ⇒ Add ⇒ JDK Commands.
TextPad Tips and Configuration
Check HERE!
1.2 Using NotePad++ for Java Programming (on
Windows)
Notepad++ (@ http://notepad-plus-plus.org) is a free and open-source programming
editor.
You can use NotePad++ to write your Java source code. But you need to use CMD shell
to compile and run the Java program.
NotePad++ Customization and Tips
You can customize Notepad++ to compile and run Java programs with hot-keys. Read
"NotePad++".
1.3 How To Set the Environment Variable JAVA_HOME
Many Java applications (such as Tomcat) require the environment variable JAVA_HOME to
be set to the JDK installed directory.
To set the JAVA_HOME environment variable:
1. First, find your JDK installed directory. For JDK 11, the default is " c:\Program
Files\Java\jdk-11.0.{x}", where "{x} is the update number. Use your "File
Explorer" to find this directory and take note of your update number {x}.
2. Check if JAVA_HOME is already set. Start a CMD and issue:
set JAVA_HOME
If you get a message "Environment variable JAVA_HOME not defined", proceed to
the next step.
If you get "JAVA_HOME=C:\Program Files\Java\jdk-11.0.{x}", verify that it is
set correctly to your JDK directory. If not, proceed to the next step.
3. To set the environment variable JAVA_HOME in Windows 10:
a. Launch "Control Panel" ⇒ (Optional) "System and Security" ⇒ "System" ⇒
Click "Advanced system settings" on the left pane.
b. Switch to "Advanced" tab ⇒ Click "Environment Variables"
c. Under "System Variables" (the bottom pane) ⇒ Click "New" (or Look for
"JAVA_HOME" and "Edit" if it is already set) ⇒ In "Variable Name", enter
"JAVA_HOME" ⇒ In "Variable Value", enter your JDK installed directory you
noted in Step 1. (In the latest Windows 10: you can push the "Browse
Directory" button and navigate to the JDK installed directory to avoid typo
error.)
4. To verify, RE-START a CMD (restart is needed to refresh the environment variables)
and issue:
5. set JAVA_HOME
javac -version
If a JDK version number is returned (e.g., JDK x.x.x), then JDK has already been
installed. If the JDK version is prior to 1.8, proceed to Step 2 to install the latest JDK;
otherwise, proceed to "Step 3: Write a Hello-world Java program".
If message "command not found" appears, JDK is NOT installed. Proceed to the
"Step 2: Install JDK".
If message "To open javac, you need a Java runtime" appears, select "Install" and
follow the instructions to install JDK. Then, proceed to "Step 3: Write a Hello-world
Java program".
Step 2: Download JDK
1. Goto Java SE download site
@ http://www.oracle.com/technetwork/java/javase/downloads/index.html.
2. Under "Java Platform, Standard Edition" ⇒ "Java SE 11.0.{x}", where {x} denotes a
fast running security-update number ⇒ Click the "Oracle JDK" "Download" button.
3. Under "Java SE Development Kit 11.0.{x}" ⇒ Check "Accept License Agreement".
4. Choose the JDK for your operating platform, i.e., MacOS. Download the DMG
installer (e.g, jdk-11.0.{x}_osx-x64_bin.dmg - about 166MB).
Step 3: Install JDK/JRE
1. Double-click the downloaded Disk Image (DMG) file. Follow the screen instructions
to install JDK/JRE.
2. Eject the DMG file.
3. To verify your installation, open a "Terminal" and issue these commands.
/usr/bin/java
$ javac -version
If a JDK version number (e.g., "javac x.x.x") appears, JDK has already been installed.
You can skip the installation and goto step 2.
$ ls -ld /etc/alternatives/java*
lrwxrwxrwx 1 root root xx xxx xx xx:xx /etc/alternatives/java ->
/usr/local/java/jdk-11.0.{x}/bin/java
lrwxrwxrwx 1 root root xx xxx xx xx:xx /etc/alternatives/javac ->
/usr/local/java/jdk-11.0.{x}/bin/javac
lrwxrwxrwx 1 root root xx xxx xx xx:xx /etc/alternatives/javaws ->
/usr/local/java/jdk-11.0.{x}/bin/javaws
Alternatively, you can include the JDK's bin and JRE's bin into the PATH directly.
15. To verify the JDK installation, issue these commands:
/usr/bin/java
30. [Don't Do this step - It is taken care by "alternative" in Step 3. Keep here to show
you how to set PATH.]
Add JDK's binary directory ("bin") to the "PATH" by editing "/etc/profile":
31. $ cd /etc
export JAVA_HOME=/usr/local/java/jdk-11.0.{x}
export PATH=$JAVA_HOME/bin:$PATH
$ echo $PATH
.....:/usr/local/java/jdk-11.0.{x}/bin
7. To compile the Hello-world Java program, launch a Terminal and issue these
commands:
SYMPTOM: Can compile but cannot run Java program from the CMD shell (e.g.,
"java Hello" does not work!)
ERROR MESSAGE (Post JDK 1.7): Error: Could not find or load main class Xxx
ERROR MESSAGE (Pre JDK 1.7): Exception in thread "main"
java.lang.NoClassDefFoundError: Xxx
PROBABLE CAUSES:
1) The Java class (in this example, Hello.class) is NOT in the current
directory.
2) The CLASSPATH environment variable is set, but does not include the
current directory ".".
POSSIBLE SOLUTIONS:
1) Issue a "dir" command to list the contents of the current directory.
Check that it contains the Java class to be run (e.g., Hello.class).
You need to compile the source program (".java") to get the class
file (".class").
2) If the Java class is present in the current directory, issue a "set
classpath" command
to check 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)
3) (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.
SYMPTOM: Can compile but cannot run the Hello-world program (e.g., "java
Hello" does not work!)
ERROR MESSAGE (Post JDK 1.7): Error: Main method not found in class Hello.
POSSIBLE SOLUTIONS: Check whether there is a main() method in your program,
and the signature of your main()
as shown in the error message.
java Hello.java
Notes:
This is applicable to single source-file only.
No need to use javac to compile the program.
It compiles in memory (without producing a .class file), and run.
This feature is introduced for beginners to learn Java, and for professionals to test a
Java feature.
The filename and classname need not be the same.
Eclipse/NetBeans
Using an IDE can greatly simplifies inclusion of external packages. Read "Eclipse How-To"
or "NetBeans How-To".
Link to References & Resources
Latest version tested: JDK 11.0.1
Last modified: March, 2019
Feedback, comments, corrections, and errata can be sent to Chua Hock-Chuan
([email protected]) | HOME