Archive for September, 2016

1. Command-line “Terminal”

Programmers uses a Command-line Shell to issue commands, this is because command-line is much more flexible and powerful than graphical interface.

The Terminal is an application that runs a shell program. By default, the Terminal in Ubuntu and Mac OS X runs the so-called bash shell.

To launch a Terminal:

In Mac OS X: Open “Finder” ⇒ Go ⇒ Utilities ⇒ Select “Terminal”. Drag the “Terminal” to your dock since you need to use it frequently or command + space bar and search for terminal and select

In Ubuntu: Open “Dash” ⇒ type “Terminal”; or choose “Applications” lens ⇒ Installed ⇒ Select “Terminal”. Drag the “Terminal” to your Launcher since you need to use it frequently.

A Terminal displays a command prompt ending with a “$” sign, in the form of:

In Mac OS X: “ComputerName:CurrentDirectoryUsername$“

In Linux: “Username@ComputerName:CurrentDirectory$“

You can enter commands after the command prompt. For example, enter “pwd” to print the current working directory:

$ pwd
.......

In this article, I shall denote the command prompt simply as “$“.

2. File System

Files and Directories (Folder)

Files are organized in directories (aka folders). The directories are organized in a hierarchical tree structure, starting from the root directory. A directory may contain sub-directories and files. A sub-directory may contain sub-sub-directories and files.

Root Directory (/)

A file is identified via the directories and filename, e.g., “/usr/lib/jvm/jdk1.7.0_07/bin/javac“. The leading “/” (forward slash) denotes the root directory. The sub-directories are also separated by a “/“.

There is only one root directory for the entire Unix’s file system. Hard drives are mounted somewhere under the root directory.

Notes: Windows use “\” (back slash) as the directory separator, and may contain multiple root directories – one for each drive (e.g., c:\, d:\).

Home Directory (~)

Unix is a multi-user operating system (although most of you, in particular the Mac users, use it as a single-user personal computer). Each user on the system is allocated a directory for storing his files, known as home directory. The users’ home directories are allocated under /Users (for Mac OS X), or /home (for Ubuntu), with a sub-directory name the same as the username, e.g. /Users/peter, /Users/paul in Mac OS (or /home/peter, /home/paul in Ubuntu).

Your home directory (/Users/<yourname>) contains sub-directories such as Downloads, Documents. Their full filenames are /Users/<yourname>/Downloads, /Users/<yourname>/Documents, respectively.

You can use a special notation “~” to denote your home directory. In other words, ~/Downloads is the same as /Users/<yourname>/Downloads.

Pathname and Filename

To reference a file, you need to provide the pathname (directory and sub-directories names) and the filename. For example, in “/usr/lib/jvm/jdk1.7.0_07/bin/javac“, the pathname is “/usr/lib/jvm/jdk1.7.0_07/bin/” and the filename is “javac“.

The pathname can be specified in two ways:

Absolute Pathname: An absolute path begins from the root directory. That is, it starts with a “/” followed by all the sub-directories, separated with “/” leading to the file, e.g., “/usr/lib/jvm/jdk1.7.0_07/bin/“.An absolute path can also begin with the current user’s home directory (starts with “~”). For example, “~/Downloads/jdk/” is the same as “/Users/<yourname>/Downloads/jdk/” in Mac OS.

Relative Pathname: A relative path is relative to the so-called current working directory. A relative path does NOT begin with “/” or “~“. For example, if the current working directory is “/usr/lib/jvm/“, then the relative pathname “jdk1.7.0_07/bin/” refers to “/usr/lib/jvm/jdk1.7.0_07/bin/“.

Unix system is case sensitive, a rose is NOT a Rose, and is NOT a ROSE.

3. Basic Commands

3.1 pwd (Print Current Working Directory)

The Terminal session maintains a so-called current working directory. All relative pathnames/filenames are relative to the current working directory. To display the current directory, issue command “pwd” (print working directory):

// Print Current Working Directory
$ pwd
......

When a Terminal is launched, it sets the initial working directory to the home directory of the current login user (denoted as “~“).

The current working directory is often included as part of the command prompt.

3.2 cd (Change Working Directory)

To change the current working directory, issue command “cd <new-pathname>“. You can specify new-pathname in two ways: absolute or relative. As explained earlier, an absolute path begins with a “/” (root directory) or “~” (home directory); whereas a relative path is relative to the current working directory and does NOT begin with “/” or “~“. For example,

You can use “/” to denote the root; “~” to refer to your home directory; “..” (double-dot) to refer to the parent directory; “.” (single-dot) to refer to the current directory; and “-” (dash) to refer to the previous directory. For example,

$ cd ~// Change directory to the home directory of the current user
$ cd// same as above, default for "cd" is home directory
$ cd ~/Documents// Change directory to the sub-directory "Documents" of the home directory of the current user
$ cd ..// Change directory to the parent directory of the current working directory
$ cd -// Change directory to the previous working directory (OLDPWD)

Setting proper working directory can greatly simplify your work. For example, to compile a Java program called “Hello.java” in “~/myproject/java/“:

Set the working directory to “~/myproject/java/“, and reference the file with filename only (without the path):

$ cd ~/myproject/java// Set the working directory
$ javac Hello.java// Filename only, in current directory

You can also refer to a file with its full pathname in any working directory:

// Any working directory
$ javac ~/myproject/java/Hello.java// Using fully-qualified filename

3.3 ls (List Directory’s Contents)

You can use command ls to list the contents of the current working directory, e.g.,

6. Programming Text Editors

A program editor (or source code editor) is programming language sensitive and context-aware. It highlights the syntax elements of your programs; and provides many features that aid in your program development (such as auto-complete, compile/build/run, help menu, etc.). On the other hand, a plain text editor is not language-sensitive and, therefore, is NOT suitable for writing programs. For full-scale software development, you should use an appropriate IDE (Integrated Development Environment).

It is important to use a mono-space font (such as “Courier”, “Consola”) for programming, so that the columns are properly aligned.

6.1 Mac’s Default Editor – TextEdit

To use the Mac’s default text editor “TextEdit” for programming, you need to choose the option “Make Plain Text” (under “Format”), before editing/saving your file.

TextEdit is NOT a programming text editor, as it does not provide syntax highlighting. You are strongly advise to install a programming editor (to be described in the following sections).

1. Command Prompt – CMD

Programmers use a Command-Line Shell to issue commands, this is because command-line is much more flexible and powerful than graphical interface.

The CMD is a Command Shell for issuing commands to interact with the Windows Operating System. It provides a set of commands and utilities. It also has its own programming language for writing batch files (shell scripts).

You can launch a CMD shell via:

“Start” button ⇒ “Run…” ⇒ Enter “cmd”, or

“Start” button ⇒ All Programs ⇒ Accessories ⇒ Command Prompt.

The CMD shell displays a prompt which ends with “>“, in the form of “drive:\current-directory>“. You can enter your command after the prompt.

In this article, I shall denote the command prompt simply as “>“.

2. File system and command

In Windows, files are organized in directories /folders. It isorganized in a hierarchical tree structure, starting from the so-called root directory for each of the hard drive (as illustrated). A directory may contain sub-directories and files. A sub-directory may contain sub-sub-directories and files, and so on.

Windows’ file system is further organized in drives, identified by a drive letter followed by a colon, e.g., C:, D: and E:. Each drive has its own root directory, such as C:\, D:\ and E:\, where the “\” (back-slash) denote the root directory of each drive.

Windows’ file system is NOT case-sensitive, a rose is a Rose, and is a ROSE.

2.1 Drive, Pathname and Filename

To reference a file in Windows’ file system, you need to provide the drive letter, the directory name (pathname) and the filename. For example, in “C:\Program Files\java\jdk1.7.0_07\bin\javac.exe“, the drive is C:, the pathname is “\Program Files\java\jdk1.7.0_07\bin\” and the filename is “javac.exe“. The leading “\” (back-slash) denotes the root directory for that drive. The sub-directories are separated by “\” (back-slash).

The pathname can be specified in two ways:

Absolute Pathname: An absolute pathname begins from the root directory of a drive. It starts with X:\ (where X denotes the drive letter and the leading “\” denotes the root), and contains all the sub-directories leading to the file (separated by “\“). For example, “C:\Program Files\java\jdk1.7.0_07\bin\“.

Relative Pathname: A relative pathname is relative to the so-called current drive and current working directory. For example, if the current drive and working directory is “C:\Program Files\java\“, then the relative path “jdk1.7.0_07\bin\” resolves to “C:\Program Files\java\jdk1.7.0_07\bin\“. A relative pathname does NOT begin with a leading “\” (back-slash).

2.2. Current Drive and Current Working Directory

Each CMD session maintains a so-called current drive and current working directory, which is shown in the prompt in the form of “drive:\current-directory>“. All relative pathnames/filenames are relative to this current drive and working directory.

2.3 Set Current Drive (x:) Command

To set or change the current drive, enter the drive letter followed by a colon (:), e.g.,

Prompt> d:// Change the current drive to D. The prompt changes to D:\...
D:\...> c:// Change the current drive to C. The prompt changes to C:\...
C:\...>

2.4 Change Directory (cd) Command

To change current working directory, under the current drive, use command “cd new-path” (change directory).

It is important to take note that you need to set the current drive first (via “x:” command) before setting the current directory under the current drive.

You can specify new-path in two ways: absolute or relative. An absolute path begins with a “\” or root directory. A relative path is relative to the current working directory and does NOT begin with a leading “\“. For example,

Prompt> c:// Set current drive to C. The prompt changes to C:\...
C:\....> cd \// Set current directory to the root directory of the current drive
C:\> cd Windows// Set current directory to "Windows" relative to current directory of the current drive
C:\Windows> cd system// Set current directory to "system" relative to current directory of the current drive
C:\Windows\system> cd \myproject\java// Set current directory absolutely to "\myproject\java" of the current drive
C:\myproject\java> cd "\Program Files\java\jdk1.7.0_07\bin"// Set current directory absolutely. Enclosed with double quotes if pathname contains blank.
C:\Program Files\java\jdk1.7.0_07\bin> d:// Set the current drive to D drive
D:\....> cd \// Change directory to the root of the current drive
D:\> cd Java// Change directory to the "Java" sub-directory of the current directory
D:\Java>

Take note that:

You need to set the current drive and current directory in two commands: X: and cd.

The current drive and current working directory is displayed in the command prompt before the “>“.

You can cd in multiple stages (e.g., one cd for each sub-directory – recommended), or cd in one single stage with the full pathname.

You could, of course, view the contents of a directory using “Computer” or “Window Explorer” more conveniently.

2.6 Shortcut Keys in CMD Shell – IMPORTANT

Previous Commands in Command History: You can use the up/down arrow keys to scroll through the previous/next command in the command history.

Auto-Complete with TAB: Type the first few characters of a filen/directory name, and press TAB key to auto-complete the file/directory name. Press TAB key repeatably to cycle through all the matches.

Copy/Paste: You need to enable Copy/Paste by clicking on the CMD icon (top-left corner) ⇒ Properties ⇒ Options ⇒ Edit Options ⇒ Check “QuickEdit Mode”. Once enabled, you can right-click to copy the highlighted text, and another right-click to paste on the command-line.

ESC: Clear command-line.

HOME|END: Move the the begin/end of command line.

Ctrl-Arrow-Left|Right: Move one word to the left/right.

Tips and Tweaks for CMD

CMD shell is NOT case-sensitive.

The screen buffer size (controlling the amount of messages retained in the screen) can be configured under “Properties” ⇒ “Layout”. You should set to a bigger number (500-2000), so that you can view more old messages.

3. Keyboard Shortcuts (for Windows)

Good programmers uses keyboard shortcuts instead of mouse to perform most of the editing tasks, such as positioning the cursor, selecting texts, copy and paste. Below are the frequently-used keyboard shortcuts for the programmers.

Keyboard Shortcut

Function

Ctrl+c, Ctrl+v, Ctrl+x

Copy, Paste, Cut

Ctrl+s

Save

Ctrl+f

Find

Ctrl+z, Ctrl+y

Undo, Redo

Ctrl+RightArrow, Ctrl+LeftArrow

Goto next/previous word

Home, End

Goto begin/end of the current line

Ctrl+Home, Ctrl+End

Goto top/end of document

Ctrl+a

Select all

Ctrl+Shift+RightArrow, Ctrl+Shift+LeftArrow

Select words

Shift+DownArrow, Shift+UpArrow

Select lines

Shift+RightArrow, Shift+LeftArrow

Select characters

Shift+End, Shift+Home

Select till end/begin of current line

Alt+Tab

Switch between open applications

Alt+F4

Close the current application

Mouse Clicks

Single-click to position the mouse pointer.

Double-click to select a word.

Triple-click to select a paragraph.

4. Source-Code Editors (for Windows)

A program editor (or source code editor) is programming language sensitive and context-aware. It highlights the syntax elements of your programs; and provides many features that aid in your program development (such as auto-complete, compile/build/run, help menu, etc.). On the other hand, a plain text editor, such as “NotePad”, is not language-sensitive and, therefore, is NOT suitable for writing programs. For full-scale software development, you should use an appropriate IDE (Integrated Development Environment).

It is important to use a mono-space font (such as “Courier New”, “Consola”) for programming, so that the columns are properly aligned.

There are tons of free program editors available. Here are some program editors that I am currently using. Unfortunately, there isn’t one magic wane (or silver bullet) that suits all my programming needs.

4.1 TextPad

TextPad is an excellent source-code editor, especially for writing toy Java programs. It is simple to use, fast to launch, and it can be configured to run JDK commands directly. TextPad is a shareware (not a freeware). The full-function version can be downloaded from www.textpad.com. It has no time limit but a message will keep reminding you to purchase the shareware.

To install, simply run the downloaded installer.

Using TextPad for Writing Toy Java Programs

TextPad is great in writing toy Java programs, as it can be configured to run JDK command directly, bypassing the command shell. From the TextPad editor, you can invoke the JDK compiler and runtime directly via “Tools” ⇒ (Version 5 only) “External Tools” ⇒ “Compile Java” or “Run Java Application”. Take note of the keyboard shortcuts.

If you cannot find these commands in the “Tools” menu, goto “Configure” ⇒ Preferences… ⇒ Tools ⇒ Add ⇒ JDK Commands. You can also configure the attributes of “compile” and “run” there, such as prompting for command-line arguments.

Tips for Using TextPad

Error Message Hyperlink: Double-click on the first-line of an error message will hyperlink to the corresponding source statement.

Find & Replace: Most important commands for programmers (after the copy/paste), available under the “Search” menu.

Block Select Mode: The Block Select mode allows you to operate on a block of texts, such as deleting a few columns and filling in blank columns. To enable/disable Block Select mode, select “Configure” menu ⇒ check/uncheck “Block Select Mode”.

Line Numbers: To display the line numbers, choose “View” menu ⇒ check the “Line Number” option.

TextPad Common Errors

ERROR MESSAGE: error: illegal character: \29
SOLUTION:
You has non-printable character "\29" (Ctrl-]) in your source code. (You probably meant to use Shift-] for '}'.)
Remove this character if you can figure out its location. Otherwise, remove the whole line and re-enter the line.

4.2 NotePad++

NotePad++ is an open-source and free program editor that supports syntax highlighting for many languages. It is meant as a “Notepad replacement”. You can download NotePad++ from http://notepad-plus-plus.org.

NotePad++ for Java Programming

To compile and run Java programs with a single hot-key (just like TextPad), I wrote two batch files called “JavaCompile.bat” and “JavaRun.bat” and save them in the NotePad++ installed directory (or a directory in the PATH).

$(FULL_CURRENT_PATH): the fully qualified path and name to the current document.

$(CURRENT_DIRECTORY): The directory the current document resides in.

$(FILE_NAME): The filename of the document, without the directory.

$(NAME_PART): The filename without the extension.

$(EXT_PART): The extension of the current document.

To modify the commands, you need to edit “shortcuts.xml” (search under your user’s AppDate directory).

Using NppExec Plugin

The above Compile-Java batch file, unfortunately, does not automatically save the current file before running the compilation. To automatically save the file, I have to use the “NppExec” Plugin, as follows:

Press “OK” to run the script. You shall see the output on the NppExec’s console.

To install a hot-key for this NppExec script:

From “Plugins” menu ⇒ Select “NppExec” ⇒ “Advanced Options…”.

In “Menu Item” ⇒ “Associated script” ⇒ Select the script “Java-Compile” ⇒ “Add/Modify”. You shall see the item appears in the “Menu Items”. If you open the NppExec menu, you will see “Java-Compile” as a menu item.

Editra

4.4 Hex Editors

At times, you may need to display the hex values of a file. A Hex Editor is a handy tool that a good programmer should have in his/her toolbox. There are many freeware/shareware Hex Editor available. Try google “Hex Editor”.