{"id":181,"date":"2020-12-04T17:42:18","date_gmt":"2020-12-04T17:42:18","guid":{"rendered":"http:\/\/bhtinstitute.com\/knowhow\/?p=181"},"modified":"2020-12-04T17:42:33","modified_gmt":"2020-12-04T17:42:33","slug":"ii-get-started-with-java-programming","status":"publish","type":"post","link":"http:\/\/bhtinstitute.com\/knowhow\/ii-get-started-with-java-programming\/","title":{"rendered":"II &#8211; Get Started with Java Programming"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\" id=\"jdk-install\">1.&nbsp;&nbsp;How To Install JDK on Windows<\/h3>\n\n\n\n<h5 class=\"wp-block-heading\">Step 0: Un-Install Older Version(s) of JDK\/JRE<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">I recommend that you install only the&nbsp;<em>latest<\/em>&nbsp;JDK. Although you can install multiple versions of JDK\/JRE concurrently, it is messy.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you have previously installed older version(s) of JDK\/JRE, un-install ALL of them. Goto &#8220;Control Panel&#8221; \u21d2 (optional) &#8220;Programs&#8221; \u21d2 &#8220;Programs and Features&#8221; \u21d2 Un-install ALL programs begin with &#8220;Java&#8221;, such as &#8220;Java SE Development Kit &#8230;&#8221;, &#8220;Java SE Runtime &#8230;&#8221;, &#8220;Java X Update &#8230;&#8221;, and etc.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Step 1: Download JDK<\/h5>\n\n\n\n<ol class=\"wp-block-list\"><li>Goto Java SE download site @&nbsp;<a href=\"http:\/\/www.oracle.com\/technetwork\/java\/javase\/downloads\/index.html\">http:\/\/www.oracle.com\/technetwork\/java\/javase\/downloads\/index.html<\/a>.<\/li><li>Under &#8220;Java Platform, Standard Edition&#8221; \u21d2 &#8220;Java SE 13.0.{x}&#8221;, where&nbsp;{x}&nbsp;denotes a fast running security-update number \u21d2 Click the &#8220;Oracle JDK Download&#8221; button.<\/li><li>Under &#8220;Java SE Development Kit 13.0.{x}&#8221; \u21d2 Check &#8220;Accept License Agreement&#8221;.<\/li><li>Choose the JDK for your operating system, i.e., &#8220;Windows&#8221;. Download the &#8220;<strong>exe<\/strong>&#8221; installer (e.g., &#8220;<code>jdk-13.0.{x}_windows-x64_bin.<strong>exe<\/strong><\/code>&#8221; &#8211; about 159MB).<\/li><\/ol>\n\n\n\n<h5 class=\"wp-block-heading\">Step 2: Install JDK<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Run the downloaded installer (e.g., &#8220;<code>jdk-13.0.{x}_windows-x64_bin.exe<\/code>&#8220;), which installs both the JDK and JRE.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By default, JDK is installed in directory &#8220;<code>C:\\Program Files\\Java\\jdk-13.0.{x}<\/code>&#8220;, where&nbsp;<code>{x}<\/code>&nbsp;denotes the update number. Accept the defaults and follow the screen instructions to install JDK.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use your &#8220;File Explorer&#8221;, navigate to&nbsp;<code>\"C:\\Program Files\\Java\"<\/code>&nbsp;to inspect the sub-directories. Take note of your&nbsp;<strong>JDK installed directory<\/strong>&nbsp;<code>jdk-13.0.{x}<\/code>, in particular, the update number&nbsp;<code>{x}<\/code>, which you will need in the next step.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I shall refer to the&nbsp;<strong>JDK installed directory<\/strong>&nbsp;as&nbsp;<strong><code>&lt;JAVA_HOME&gt;<\/code><\/strong>, hereafter, in this article.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"jdk-install-step3\">Step 3: Include JDK&#8217;s &#8220;bin&#8221; Directory in the&nbsp;PATH<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Windows&#8217; Command Prompt (<code>CMD<\/code>) searches the current directory and the directories listed in the&nbsp;<code>PATH<\/code>&nbsp;<em>environment variable<\/em>&nbsp;(or&nbsp;<em>system variable<\/em>) for executable programs. JDK&#8217;s programs (such as Java compiler&nbsp;<code>\"javac.exe<\/code>&#8221; and Java runtime&nbsp;<code>\"java.exe<\/code>&#8220;) reside in the&nbsp;<em>sub-directory<\/em>&nbsp;&#8220;<code>bin<\/code>&#8221; of the JDK installed directory. You need to include JDK&#8217;s &#8220;<code>bin<\/code>&#8221; in the&nbsp;<code>PATH<\/code>&nbsp;to run the JDK programs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To edit the&nbsp;<code>PATH<\/code>&nbsp;environment variable in Windows 10:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Launch &#8220;Control Panel&#8221; \u21d2 (Optional) &#8220;System and Security&#8221; \u21d2 &#8220;System&#8221; \u21d2 Click &#8220;Advanced system settings&#8221; on the left pane.<\/li><li>Switch to &#8220;Advanced&#8221; tab \u21d2 Click &#8220;Environment Variables&#8221; button.<\/li><li>Under &#8220;System Variables&#8221; (the bottom pane), scroll down to select variable &#8220;Path&#8221; \u21d2 Click &#8220;Edit&#8230;&#8221;.<\/li><li><strong>For Newer Windows 10<\/strong>:<br>You shall see a&nbsp;<strong>TABLE<\/strong>&nbsp;listing all the existing PATH entries (if not, goto next step). Click &#8220;New&#8221; \u21d2 Click &#8220;Browse&#8221; and navigate to your JDK&#8217;s &#8220;<code>bin<\/code>&#8221; directory, i.e., &#8220;<code>c:\\Program Files\\Java\\jdk-13.0.{x}\\<strong>bin<\/strong><\/code>&#8220;, where&nbsp;<code>{x}<\/code>&nbsp;is your installation update number \u21d2 Select &#8220;Move Up&#8221; to move this entry all the way to the TOP.<\/li><li><strong>For Older Windows 10<\/strong>&nbsp;(Time to change your computer!):<br><strong>(CAUTION: Read this paragraph 3 times before doing this step! Don&#8217;t push &#8220;Apply&#8221; or &#8220;OK&#8221; until you are 101% sure. There is no UNDO!!!)<\/strong><br>(To be SAFE, copy the content of the &#8220;Variable value&#8221; to Notepad before changing it!!!)<br>In &#8220;Variable value&#8221; field,&nbsp;APPEND&nbsp;&#8220;<code>c:\\Program Files\\Java\\jdk-13.0.{x}\\bin<\/code>&#8221; (where&nbsp;<code>{x}<\/code>&nbsp;is your installation update number)&nbsp;IN FRONT&nbsp;of all the existing directories,&nbsp;followed by a semi-colon (<code>;<\/code>)&nbsp;to separate the JDK&#8217;s bin directory from the rest of the existing directories.&nbsp;DO NOT DELETE&nbsp;any existing entries; otherwise, some existing applications may not run.Variable name : <strong>PATH<\/strong> Variable value : <strong>c:\\Program Files\\Java\\jdk-13.0.{x}\\bin;<\/strong><strong>[<em>do not delete exiting entries&#8230;<\/em>]<\/strong><\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Note: If you have started CMD, you need to re-start for the new environment settings to take effect.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Step 4: Verify the JDK Installation<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Launch a&nbsp;<code>CMD<\/code>&nbsp;via one of the following means:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Click &#8220;Search&#8221; button \u21d2 Type &#8220;cmd&#8221; \u21d2 Choose &#8220;Command Prompt&#8221;, or<\/li><li>Right-click &#8220;Start&#8221; button \u21d2 run&#8230; \u21d2 enter &#8220;cmd&#8221;, or<\/li><li>Click &#8220;Start&#8221; button \u21d2 Windows System \u21d2 Command Prompt<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Issue the following commands to verify your JDK installation:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Issue &#8220;<code>path<\/code>&#8221; command to list the contents of the&nbsp;<code>PATH<\/code>&nbsp;environment variable. Check to make sure that your JDK&#8217;s &#8220;<code>bin<\/code>&#8221; is listed in the&nbsp;<code>PATH<\/code>.<strong>path<\/strong> PATH=c:\\Program Files\\Java\\jdk-13.0.{x}\\bin;other entries&#8230;<\/li><li>Issue the following commands to verify that JDK\/JRE are properly installed and display their version:\/\/ Display the JDK version <strong>javac -version<\/strong> javac 13.0.1 \/\/ Display the JRE version <strong>java -version<\/strong> java version &#8220;13.0.1&#8221; 2019-10-15 Java(TM) SE Runtime Environment (build 13.0.1+9) Java HotSpot(TM) 64-Bit Server VM (build 13.0.1+9, mixed mode, sharing)<\/li><\/ol>\n\n\n\n<h5 class=\"wp-block-heading\">Step 5: Write a Hello-World Java Program<\/h5>\n\n\n\n<ol class=\"wp-block-list\"><li>Create a directory to keep your works, e.g., &#8220;<code>d:\\myProject<\/code>&#8221; or &#8220;<code>c:\\myProject<\/code>&#8220;. Do NOT save your works in &#8220;Desktop&#8221; or &#8220;Documents&#8221; as they are hard to locate. The directory name shall not contain&nbsp;<em>blank<\/em>&nbsp;or special characters. Use meaningful but short name as it is easier to type.<\/li><li>Launch a programming text editor (such as&nbsp;<a href=\"https:\/\/www3.ntu.edu.sg\/home\/ehchua\/programming\/howto\/CMD_Survival.html#textpad\">TextPad<\/a>,&nbsp;<a href=\"https:\/\/www3.ntu.edu.sg\/home\/ehchua\/programming\/howto\/CMD_Survival.html#NotePadPP\">NotePad++<\/a>, Sublime Text, Atom). Begin with a&nbsp;<em>new file<\/em>&nbsp;and enter the following source code. Save the file as &#8220;<code>Hello.java<\/code>&#8220;, under your work directory (e.g.,&nbsp;<code>d:\\myProject<\/code>).\/* * First Java program to say Hello *\/ public class Hello { \/\/ Save as &#8220;Hello.java&#8221; under &#8220;d:\\myProject&#8221; public static void main(String[] args) { System.out.println(&#8220;Hello, world!&#8221;); } }<\/li><\/ol>\n\n\n\n<h5 class=\"wp-block-heading\">Step 6: Compile and Run the Hello-World Java Program<\/h5>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"505\" height=\"753\" src=\"http:\/\/bhtinstitute.com\/knowhow\/wp-content\/uploads\/2020\/12\/JavaBasics_GettingStarted.png\" alt=\"JavaBasics_GettingStarted.png\" class=\"wp-image-182\" srcset=\"http:\/\/bhtinstitute.com\/knowhow\/wp-content\/uploads\/2020\/12\/JavaBasics_GettingStarted.png 505w, http:\/\/bhtinstitute.com\/knowhow\/wp-content\/uploads\/2020\/12\/JavaBasics_GettingStarted-201x300.png 201w, http:\/\/bhtinstitute.com\/knowhow\/wp-content\/uploads\/2020\/12\/JavaBasics_GettingStarted-40x60.png 40w, http:\/\/bhtinstitute.com\/knowhow\/wp-content\/uploads\/2020\/12\/JavaBasics_GettingStarted-101x150.png 101w\" sizes=\"auto, (max-width: 505px) 100vw, 505px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">To compile the source code &#8220;<code>Hello.java<\/code>&#8220;:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Start a CMD Shell (Search \u21d2 enter &#8220;cmd&#8221; \u21d2 select &#8220;Command Prompt&#8221;).<\/li><li>Set the&nbsp;<em>Current Drive<\/em>&nbsp;to the drive where you saved your source file &#8220;<code>Hello.java<\/code>&#8220;.<br>If you use drive &#8220;c&#8221;, skip this step.<br>Else if you use drive &#8220;<code>d<\/code>&#8220;, enter &#8220;<code><strong>d:<\/strong><\/code>&#8221; as follow:<strong>d:<\/strong> D:\\xxx&gt;<\/li><li>Set the&nbsp;<em>Current Working Directory<\/em>&nbsp;to the directory that you saved your source file via the&nbsp;<strong><code>cd<\/code><\/strong>&nbsp;(<em>Change Directory<\/em>) command. For example, suppose that your source file is saved in directory &#8220;<code>myProject<\/code>&#8220;.<br><strong>cd \\myProject<\/strong> D:\\myProject&gt;<\/li><li>Issue a&nbsp;<strong><code>dir<\/code><\/strong>&nbsp;(<em>List Directory<\/em>) command to confirm that your source file is present in the&nbsp;<em>current directory<\/em>.<strong>dir<\/strong> &#8230;&#8230; xx-xxx-xx xx:xx PM 277 <strong>Hello.java<\/strong> &#8230;&#8230;<\/li><li>Invoke the JDK compiler &#8220;<strong><code>javac<\/code><\/strong>&#8221; to compile the source code &#8220;<code>Hello.java<\/code>&#8220;.<strong>javac Hello.java<\/strong> \/\/ If error message appears, correct your source code and re-compileThe 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 &#8220;<a href=\"https:\/\/www3.ntu.edu.sg\/home\/ehchua\/programming\/howto\/ErrorMessages.html#JDKErrors\">Common JDK Installation Errors<\/a>&#8220;, if you encounter problem compiling your program.<\/li><li>The output of the compilation is a Java class called &#8220;<code>Hello.class<\/code>&#8220;. Issue a&nbsp;<strong><code>dir<\/code><\/strong>&nbsp;(<em>List Directory<\/em>) command again to check for the output.<strong>dir<\/strong> &#8230;&#8230; xx-xxx-xx xx:xx PM 416 <strong>Hello.class<\/strong> xx-xxx-xx xx:xx PM 277 Hello.java &#8230;&#8230;<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">To run the program, invoke the Java Runtime &#8220;<code><strong>java<\/strong><\/code>&#8220;:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>java Hello<\/strong>\nHello, world!<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Everything that can possibly go wrong will go wrong<\/strong>: Read &#8220;<a href=\"https:\/\/www3.ntu.edu.sg\/home\/ehchua\/programming\/howto\/ErrorMessages.html#JDKErrors\">JDK Installation Common Errors<\/a>&#8220;.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Step 7: (For Advanced Users Only) JDK&#8217;s Source Code<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Source code for JDK is provided and kept in &#8220;<code>&lt;JAVA_HOME&gt;\\lib\\src.zip<\/code>&#8221; (or &#8220;<code>&lt;JAVA_HOME&gt;\\src.zip<\/code>&#8221; prior to JDK 9). I strongly recommend that you to go through some of the source files such as &#8220;<code>String.java<\/code>&#8220;, &#8220;<code>Math.java<\/code>&#8220;, and &#8220;<code>Integer.java<\/code>&#8220;, under &#8220;<code>java\\lang<\/code>&#8220;, to learn how experts program.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.&nbsp;&nbsp;How To Install JDK on Windows Step 0: Un-Install Older Version(s) of JDK\/JRE I recommend that you install only the&nbsp;latest&nbsp;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 &#8220;Control Panel&#8221; \u21d2 (optional) &#8220;Programs&#8221; \u21d2 &#8220;Programs and Features&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":183,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-181","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/bhtinstitute.com\/knowhow\/wp-json\/wp\/v2\/posts\/181","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bhtinstitute.com\/knowhow\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bhtinstitute.com\/knowhow\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bhtinstitute.com\/knowhow\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/bhtinstitute.com\/knowhow\/wp-json\/wp\/v2\/comments?post=181"}],"version-history":[{"count":1,"href":"http:\/\/bhtinstitute.com\/knowhow\/wp-json\/wp\/v2\/posts\/181\/revisions"}],"predecessor-version":[{"id":184,"href":"http:\/\/bhtinstitute.com\/knowhow\/wp-json\/wp\/v2\/posts\/181\/revisions\/184"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/bhtinstitute.com\/knowhow\/wp-json\/wp\/v2\/media\/183"}],"wp:attachment":[{"href":"http:\/\/bhtinstitute.com\/knowhow\/wp-json\/wp\/v2\/media?parent=181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bhtinstitute.com\/knowhow\/wp-json\/wp\/v2\/categories?post=181"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bhtinstitute.com\/knowhow\/wp-json\/wp\/v2\/tags?post=181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}