Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d911e39

Browse files
committed
Update README.md
1 parent 5ef9863 commit d911e39

File tree

1 file changed

+339
-10
lines changed

1 file changed

+339
-10
lines changed

README.md

Lines changed: 339 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,313 @@
1-
# Example Source Code for the Book "On Java 8" by Bruce Eckel
1+
# Examples for *On Java 8* by Bruce Eckel
22

3-
Download the latest release from [here](https://github.com/BruceEckel/OnJava8-Examples/releases/).
3+
If you want to experiment with the code examples from the book [On Java
4+
8](https://www.onjava8.com/), you're in the right place.
45

5-
To compile and run these programs, you only need JDK 8 installed.
6-
Invoking `gradlew` will automatically download and install Gradle.
7-
Gradle will also install all additional libraries necessary to compile
8-
and run the Java examples in the book.
6+
These examples are automatically extracted directly from the book. This repository
7+
includes tests to verify that the code in the book is correct.
8+
9+
## Contents
10+
11+
- [Building From the Command Line: Quick Version](#building-from-the-command-line-quick-version)
12+
- [Building From the Command Line: Detailed Instructions](#building-from-the-command-line-detailed-instructions)
13+
* [Install Java](#install-java)
14+
+ [Windows](#windows)
15+
+ [Macintosh](#macintosh)
16+
+ [Linux](#linux)
17+
* [Verify Your Installation](#verify-your-installation)
18+
* [Installing and Running the Book Examples](#installing-and-running-the-book-examples)
19+
- [Appendix A: Command-Line Basics](#appendix-a-command-line-basics)
20+
* [Editors](#editors)
21+
* [The Shell](#the-shell)
22+
+ [Starting a Shell](#starting-a-shell)
23+
+ [Directories](#directories)
24+
+ [Basic Shell Operations](#basic-shell-operations)
25+
+ [Unpacking a Zip Archive](#unpacking-a-zip-archive)
26+
- [Appendix B: Testing](#appendix-b-testing)
27+
- [Troubleshooting](#troubleshooting)
28+
29+
# Building From the Command Line: Quick Version
30+
31+
Before you can run the examples from this repository, you must install
32+
[JDK8](http://www.oracle.com/technetwork/java/javase/downloads/index.html), the
33+
*Java Development Kit* for version 8 of the language.
34+
35+
If you just want to download and check the code, [Download
36+
Here](https://github.com/BruceEckel/OnJava8-Examples/archive/refs/heads/master.zip)
37+
and [unzip it](#unpacking-a-zip-archive) into your destination directory. Open
38+
a [shell/command window](#appendix-a-command-line-basics) and move into the
39+
root of that directory. You'll know you are in the right directory if you see
40+
the files `gradlew` and `gradlew.bat`.
41+
42+
You'll need an Internet connection the first time you compile the code,
43+
because Gradle needs to first install itself, then all the support libraries.
44+
Once these are installed you can perform additional compiling and running
45+
offline.
46+
47+
On Mac/Linux, enter:
48+
49+
```
50+
./gradlew test
51+
```
52+
53+
(If you get a *Permission denied* error, run `chmod +x ./gradlew`)
54+
55+
On Windows, enter
56+
57+
```
58+
gradlew test
59+
```
60+
61+
If all goes well, the tests will run. Everything should complete without errors.
62+
63+
All the book examples are in the subdirectory `Examples` in subdirectories
64+
corresponding to the atom names.
65+
66+
# Building From the Command Line: Detailed Instructions
67+
68+
If you are not familiar with the command line, first read [Command-Line
69+
Basics](#appendix-a-command-line-basics).
70+
71+
## Install Java
72+
73+
You must first install the *Java Development Kit* (JDK).
74+
75+
### Windows
76+
77+
1. Follow the instructions to [install Chocolatey](https://chocolatey.org/).
78+
79+
2. At a [shell prompt](#appendix-a-command-line-basics), type: `choco install
80+
jdk8` (you may also select a more recent version, like `jdk11`). The
81+
installation process takes some time, but when it's finished Java is installed
82+
and the necessary environment variables are set.
83+
84+
### Macintosh
85+
86+
The Mac comes with a much older version of Java that won't work for the
87+
examples in this book, so you'll need to update it to (at least) Java 8.
88+
89+
1. Follow the instructions at this link to [Install HomeBrew](http://brew.sh/)
90+
91+
2. At a [shell prompt](#appendix-a-command-line-basics), first type
92+
`brew update`. When that completes, enter `brew cask install java`.
93+
94+
**NOTE:** Sometimes the default version of Java that you get with the above
95+
installation will be too recent and not validated by the Mac's security
96+
system. If this happens you'll either need to turn off the security by hand
97+
or install an earlier version of Java. For either choice, you'll need to Google
98+
for answers on how to solve the problem (often the easiest approach is to just
99+
search for the error message produced by the Mac).
100+
101+
### Linux
102+
103+
Use the standard package installer with the following [shell commands](#appendix-a-command-line-basics):
104+
105+
*Ubuntu/Debian*:
106+
107+
1. `sudo apt-get update`
108+
109+
2. `sudo apt-get install default-jdk`
110+
111+
*Fedora/Redhat*:
112+
113+
```
114+
su -c "yum install java-1.8.0-openjdk"
115+
```
116+
117+
## Verify Your Installation
118+
119+
[Open a new shell](#appendix-a-command-line-basics) and type:
120+
121+
```
122+
java -version
123+
```
124+
125+
You should see something like the following (Version numbers and actual text
126+
will vary):
127+
128+
```
129+
openjdk version "11" 2018-09-25
130+
OpenJDK Runtime Environment 18.9 (build 11+28)
131+
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)
132+
```
133+
134+
If you see a message that the command is not found or not recognized, review
135+
the installation instructions. If you still can't get it to work, check
136+
[StackOverflow](http://stackoverflow.com/search?q=installing+java).
137+
138+
## Installing and Running the Book Examples
139+
140+
Once you have Java installed, the process to install and run the book examples
141+
is the same for all platforms:
142+
143+
1. Download the book examples from the
144+
[GitHub Repository](https://github.com/BruceEckel/OnJava8-Examples/archive/refs/heads/master.zip).
145+
146+
2. [Unzip](#unpacking-a-zip-archive) the downloaded file into the directory of your choice.
147+
148+
3. Use the Windows Explorer, the Mac Finder, or Nautilus or equivalent on Linux
149+
to browse to the directory where you uzipped `OnJava8-Examples`, and
150+
[open a shell](#appendix-a-command-line-basics) there.
151+
152+
4. If you're in the right directory, you should see files named `gradlew` and
153+
`gradlew.bat` in that directory, along with numerous other files and
154+
directories. The directories correspond to the chapters in the book.
155+
156+
5. At the shell prompt, type `gradlew test` (Windows) or `./gradlew test`
157+
(Mac/Linux).
158+
159+
The first time you do this, Gradle will install itself and numerous other
160+
packages, so it will take some time. After everything is installed, subsequent
161+
builds and runs will be much faster.
162+
163+
Note that you must be connected to the Internet the first time you run `gradlew`
164+
so that Gradle can download the necessary packages.
165+
166+
# Appendix A: Command-Line Basics
167+
168+
Because it is possible for a "dedicated beginner" to learn programming from
169+
this book, you may not have previously used your computer's command-line shell.
170+
If you have, you can go directly to the
171+
[installation instructions](#building-from-the-command-line-detailed-instructions).
172+
173+
## Editors
174+
175+
To create and modify Java program files—the code listings shown in this
176+
book—you need a program called an *editor*. You'll also need the editor to
177+
make changes to your system configuration files, which is sometimes required
178+
during installation.
179+
180+
Programming editors vary from heavyweight *Integrated Development Environments*
181+
(IDEs, like Eclipse, NetBeans and IntelliJ IDEA) to more basic text
182+
manipulation applications. If you already have an IDE and are comfortable with
183+
it, feel free to use that for this book.
184+
185+
Numerous explanations in this book are specific to IntelliJ IDEA so if you
186+
don't already have an IDE you might as well start with IDEA. There are many
187+
other editors; these are a subculture unto themselves and people sometimes get
188+
into heated arguments about their merits. If you find one you like better, it's
189+
not too hard to change. The important thing is to choose one and get
190+
comfortable with it.
191+
192+
## The Shell
193+
194+
If you haven't programmed before, you might be unfamiliar with your operating
195+
system *shell* (also called the *command prompt* in Windows). The shell harkens
196+
back to the early days of computing when everything happened by typing commands
197+
and the computer responded by displaying responses—everything was text-based.
198+
199+
Although it can seem primitive in the age of graphical user interfaces, a shell
200+
provides a surprising number of valuable features.
201+
202+
To learn more about your shell than we cover here, see
203+
[Bash Shell](https://en.wikipedia.org/wiki/Bash_(Unix_shell)) for Mac/Linux
204+
or [Windows Shell](https://en.wikipedia.org/wiki/Windows_shell).
205+
206+
### Starting a Shell
207+
208+
**Mac**: Click on the *Spotlight* (the magnifying-glass icon in the upper-right
209+
corner of the screen) and type "terminal." Click on the application that looks
210+
like a little TV screen (you might also be able to hit "Return"). This starts a
211+
shell in your home directory.
212+
213+
**Windows**: First, start the Windows Explorer to navigate through your
214+
directories:
215+
216+
- *Windows 7*: click the "Start" button in the lower left corner of the screen.
217+
In the Start Menu search box area type "explorer" and then press the "Enter"
218+
key.
219+
220+
- *Windows 8*: click Windows+Q, type "explorer" and then press the "Enter" key.
221+
222+
- *Windows 10*: click Windows+E.
223+
224+
Once the Windows Explorer is running, move through the folders on your computer
225+
by double-clicking on them with the mouse. Navigate to the desired folder. Now
226+
click the file tab at the top left of the Explorer window and select "Open
227+
Windows Powershell." This opens a shell in the destination directory.
228+
229+
**Linux**: To open a shell in your home directory:
230+
231+
- *Debian*: Press Alt+F2. In the dialog that pops up, type 'gnome-terminal'
232+
233+
- *Ubuntu*: Either right-click on the desktop and select 'Open Terminal', or
234+
press Ctrl+Alt+T
235+
236+
- *Redhat*: Right-click on the desktop and select 'Open Terminal'
237+
238+
- *Fedora*: Press Alt+F2. In the dialog that pops up, type 'gnome-terminal'
239+
240+
241+
### Directories
242+
243+
*Directories* are one of the fundamental elements of a shell. Directories hold
244+
files, as well as other directories. Think of a directory as a tree with
245+
branches. If `books` is a directory on your system and it has two other
246+
directories as branches, for example `math` and `art`, we say that you have a
247+
directory `books` with two *subdirectories* `math` and `art`. We refer to them
248+
as `books/math` and `books/art` since `books` is their *parent* directory.
249+
Note that Windows uses backslashes rather than forward slashes to separate the
250+
parts of a directory.
251+
252+
### Basic Shell Operations
253+
254+
The shell operations shown here are approximately identical across operating
255+
systems. For the purposes of this book, here are the essential operations in a
256+
shell:
257+
258+
- **Change directory**: Use `cd` followed by the name of the
259+
directory where you want to move, or `cd ..` if you want to move
260+
up a directory. If you want to move to a different directory while
261+
remembering where you came from, use `pushd` followed by the different
262+
directory name. Then, to return to the previous directory, just say
263+
`popd`.
264+
265+
- **Directory listing**: `ls` (`dir` in Windows) displays all the files and
266+
subdirectory names in the current directory. Use the wildcard `*` (asterisk) to
267+
narrow your search. For example, if you want to list all the files ending in
268+
".kt," you say `ls *.kt` (Windows: `dir *.kt`). If you want to list the
269+
files starting with "F" and ending in ".kt," you say `ls F*.kt` (Windows:
270+
`dir F*.kt`).
271+
272+
- **Create a directory**: use the `mkdir` ("make directory") command
273+
(Windows: `md`), followed by the name of the directory you want to create.
274+
For example, `mkdir books` (Windows: `md books`).
275+
276+
- **Remove a file**: Use `rm` ("remove") followed by the name of the file
277+
you wish to remove (Windows: `del`). For example, `rm somefile.kt` (Windows:
278+
`del somefile.kt`).
279+
280+
- **Remove a directory**: use the `rm -r` command to remove the files in
281+
the directory and the directory itself (Windows: `deltree`). For example,
282+
`rm -r books` (Windows: `deltree books`).
283+
284+
- **Repeat a command**: The "up arrow" on all three operating
285+
systems moves through previous commands so you can edit and
286+
repeat them. On Mac/Linux, `!!` repeats the last command and
287+
`!n` repeats the nth command.
288+
289+
- **Command history**: Use `history` in Mac/Linux or press the F7 key in Windows.
290+
This gives you a list of all the commands you've entered. Mac/Linux provides
291+
numbers to refer to when you want to repeat a command.
292+
293+
### Unpacking a Zip Archive
294+
295+
A file name ending with `.zip` is an archive containing other files in a
296+
compressed format. Both Linux and Mac have command-line `unzip` utilities, and
297+
it's possible to install a command-line `unzip` for Windows via the Internet.
298+
299+
However, in all three systems the graphical file browser (Windows Explorer, the
300+
Mac Finder, or Nautilus or equivalent on Linux) will browse to the directory
301+
containing your zip file. Then right-mouse-click on the file and select "Open"
302+
on the Mac, "Extract Here" on Linux, or "Extract all ..." on Windows.
303+
304+
# Appendix B: Testing
305+
306+
The test system is built in so that we (the authors) can verify the correctness
307+
of what goes into the book.
308+
309+
You don't need to run the tests, but if you want to, you can just run `gradlew
310+
test` (on Windows) or `./gradlew test` (Mac/Linux).
9311

10312
To compile and run everything, the command is:
11313

@@ -32,12 +334,39 @@ program in the **strings** chapter subdirectory:
32334

33335
`gradlew :strings:ReplacingStringTokenizer`
34336

35-
However, if the file name is unique throughout the book (the majority are), you can just give the
36-
program name, like this:
337+
However, if the file name is unique throughout the book (the majority are), you
338+
can just give the program name, like this:
37339

38340
`gradlew ReplacingStringTokenizer`
39341

40-
Note that all commands are run from the base directory where the example code is installed, and where you find the
41-
`gradlew` script.
342+
Note that all commands are run from the base directory where the example code is
343+
installed, and where you find the `gradlew` script.
42344

43345
You can learn about other options by just typing `gradlew` with no arguments.
346+
347+
# Troubleshooting
348+
349+
If any terminology or processes described here remain unclear to you, you can
350+
usually find explanations or answers through [Google](https://www.google.com/).
351+
For more specific issues or problems, try
352+
[StackOverflow](http://stackoverflow.com/). Sometimes you can find installation
353+
instructions on [YouTube](https://www.youtube.com/).
354+
355+
Sometimes a Gradle build will be unable to connect to the internet and download
356+
the necessary components, producing an error message containing:
357+
358+
```
359+
javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
360+
```
361+
362+
Normally this means you have multiple Java installations on your machine
363+
(applications built with Java ordinarily install their own version of Java), and
364+
somehow the `cacerts` security file is interfering with the `cacerts` file for
365+
the Java you have installed. It can be difficult to know which `cacerts` file is
366+
interfering with yours. The brute-force approach is to search for all the
367+
`cacerts` files on your machine and begin uninstalling the associated
368+
applications---or in some cases, simply removing the directory containing the
369+
`cacerts` file---until the Gradle build begins to work. You might also need to
370+
adjust some environment variables and/or your path. Once you get the Gradle
371+
build working successfully, you should be able to reinstall any applications you
372+
removed in the process.

0 commit comments

Comments
 (0)