百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
O

OnJava8-Examples

> 编程语言
开源

《关于 Java 8》一书的代码示例

3.2K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

《关于 Java 8》一书的代码示例

Examples for On Java 8 by Bruce Eckel

If you want to experiment with the code examples from the book On Java 8, you're in the right place.

These examples are automatically extracted directly from the book. This repository includes tests to verify that the code in the book is correct.

NOTE: Do not attempt to use JDK 16 or greater with gradle. This produces a BUG! message from Gradle, which is broken for those versions.

Contents

  • Building From the Command Line: Quick Version
  • Building From the Command Line: Detailed Instructions
    • Install Java
      • Windows
      • Macintosh
      • Linux
    • Verify Your Installation
    • Installing and Running the Book Examples
  • Appendix A: Command-Line Basics
    • Editors
    • The Shell
      • Starting a Shell
      • Directories
      • Basic Shell Operations
      • Unpacking a Zip Archive
  • Appendix B: Testing
  • Appendix C: Troubleshooting

Building From the Command Line: Quick Version

Before you can run the examples from this repository, you must install JDK8, the Java Development Kit for version 8 of the language.

If you just want to download and check the code, Download Here and unzip it into your destination directory. Open a shell/command window and move into the root of that directory. You'll know you are in the right directory if you see the files gradlew and gradlew.bat.

You'll need an Internet connection the first time you compile the code, because Gradle needs to first install itself, then all the support libraries. Once these are installed you can perform additional compiling and running offline.

On Mac/Linux, enter:

./gradlew test

(If you get a Permission denied error, run chmod +x ./gradlew)

On Windows, enter

gradlew test

If all goes well, the tests will run. Everything should complete without errors.

All the book examples are in the subdirectory Examples in subdirectories corresponding to the atom names.

Building From the Command Line: Detailed Instructions

If you are not familiar with the command line, first read Command-Line Basics.

Install Java

You must first install the Java Development Kit (JDK).

Windows

  1. Follow the instructions to install Chocolatey.

  2. At a shell prompt, type: choco install jdk8 (you may also select a more recent version, like jdk11). The installation process takes some time, but when it's finished Java is installed and the necessary environment variables are set.

Macintosh

The Mac comes with a much older version of Java that won't work for the examples in this book, so you'll need to update it to (at least) Java 8.

  1. Follow the instructions at this link to Install HomeBrew

  2. At a shell prompt, first type brew update. When that completes, enter brew cask install java.

NOTE: Sometimes the default version of Java that you get with the above installation will be too recent and not validated by the Mac's security system. If this happens you'll either need to turn off the security by hand or install an earlier version of Java. For either choice, you'll need to Google for answers on how to solve the problem (often the easiest approach is to just search for the error message produced by the Mac).

Linux

Use the standard package installer with the following shell commands:

Ubuntu/Debian:

  1. sudo apt-get update

  2. sudo apt-get install default-jdk

Fedora/Redhat:

su -c "yum install java-1.8.0-openjdk"

Verify Your Installation

Open a new shell and type:

java -version

You should see something like the following (Version numbers and actual text will vary):

openjdk version "11" 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)

If you see a message that the command is not found or not recognized, review the installation instructions. If you still can't get it to work, check StackOverflow.

Installing and Running the Book Examples

Once you have Java installed, the process to install and run the book examples is the same for all platforms:

  1. Download the book examples from the GitHub Repository.

  2. Unzip the downloaded file into the directory of your choice.

  3. Use the Windows Explorer, the Mac Finder, or Nautilus or equivalent on Linux to browse to the directory where you uzipped OnJava8-Examples, and open a shell there.

  4. If you're in the right directory, you should see files named gradlew and gradlew.bat in that directory, along with numerous other files and directories. The directories correspond to the chapters in the book.

  5. At the shell prompt, type gradlew test (Windows) or ./gradlew test (Mac/Linux).

The first time you do this, Gradle will install itself and numerous other packages, so it will take some time. After everything is installed, subsequent builds and runs will be much faster.

Note that you must be connected to the Internet the first time you run gradlew so that Gradle can download the necessary packages.

Appendix A: Command-Line Basics

Because it is possible for a "dedicated beginner" to learn programming from this book, you may not have previously used your computer's command-line shell. If you have, you can go directly to the installation instructions.

Editors

To create and modify Java program files—the code listings shown in this book—you need a program called an editor. You'll also need the editor to make changes to your system configuration files, which is sometimes required during installation.

Programming editors vary from heavyweight Integrated Development Environments (IDEs, like Eclipse, NetBeans and IntelliJ IDEA) to more basic text manipulation applications. If you already have an IDE and are comfortable with it, feel free to use that for this book.

Numerous explanations in this book are specific to IntelliJ IDEA so if you don't already have an IDE you might as well start with IDEA. There are many other editors; these are a subculture unto themselves and people sometimes get into heated arguments about their merits. If you find one you like better, it's not too hard to change. The important thing is to choose one and get comfortable with it.

The Shell

If you haven't programmed before, you might be unfamiliar with your operating system shell (also called the command prompt in Windows). The shell harkens back to the early days of computing when everything happened by typing commands and the computer responded by displaying responses—everything was text-based.

Although it can seem primitive in the age of graphical user interfaces, a shell provides a surprising number of valuable features.

To learn more about your shell than we cover here, see Bash Shell for Mac/Linux or Windows Shell.

Starting a Shell

Mac: Click on the Spotlight (the magnifying-glass icon in the upper-right corner of the screen) and type "terminal." Click on the application that looks like a little TV screen (you might also be able to hit "Return"). This starts a shell in your home directory.

Windows: First, start the Windows Explorer to navigate through your directories:

  • Windows 7: click the "Start" button in the lower left corner of the screen. In the Start Menu search box area type "explorer" and then press the "Enter" key.

  • Windows 8: click Windows+Q, type "explorer" and then press the "Enter" key.

  • Windows 10: click Windows+E.

Once the Windows Explorer is running, move through the folders on your computer by double-clicking on them with the mouse. Navigate to the desired folder. Now click the file tab at the top left of the Explorer window and select "Open Windows Powershell." This opens a shell in the destination directory.

Linux: To open a shell in your home directory:

  • Debian: Press Alt+F2. In the dialog that pops up, type 'gnome-terminal'

  • Ubuntu: Either right-click on the desktop and select 'Open Terminal', or press Ctrl+Alt+T

  • Redhat: Right-click on the desktop and select 'Open Terminal'

  • Fedora: Press Alt+F2. In the dialog that pops up, type 'gnome-terminal'

Directories

Directories are one of the fundamental elements of a shell. Directories hold files, as well as other directories. Think of a directory as a tree with branches. If books is a directory on your system and it has two other directories as branches, for example math and art, we say that you have a directory books with two subdirectories math and art. We refer to them as books/math and books/art since books is their parent directory. Note that Windows uses backslashes rather than forward slashes to separate the parts of a directory.

Basic Shell Operations

The shell operations shown here are approximately identical across operating systems. For the purposes of this book, here are the essential operations in a shell:

  • Change directory: Use cd followed by the name of the directory where you want to move, or cd .. if you want to move up a directory. If you want to move to a different directory while remembering where you came from, use pushd followed by the different directory name. Then, to return to the previous directory, just say popd.

  • Directory listing: ls (dir in Windows) displays all the files and subdirectory names in the current directory. Use the wildcard * (asterisk) to narrow your search. For example, if you want to list all the files ending in ".kt," you say ls *.kt (Windows: dir *.kt). If you want to list the files starting with "F" and ending in ".kt," you say ls F*.kt (Windows: dir F*.kt).

  • Create a directory: use the mkdir ("make directory") command (Windows: md), followed by the name of the directory you want to create. For example, mkdir books (Windows: md books).

  • Remove a file: Use rm ("remove") followed by the name of the file you wish to remove (Windows: del). For example, rm somefile.kt (Windows: del somefile.kt).

  • Remove a directory: use the rm -r command to remove the files in the directory and the directory itself (Windows: deltree). For example, rm -r books (Windows: deltree books).

  • Repeat a command: The "up arrow" on all three operating systems moves through previous commands so you can edit and repeat them. On Mac/Linux, !! repeats the last command and !n repeats the nth command.

  • Command history: Use history in Mac/Linux or press the F7 key in Windows. This gives you a list of all the commands you've entered. Mac/Linux provides numbers to refer to when you want to repeat a command.

Unpacking a Zip Archive

A file name en

GitHub Issues· 18 开放

在 GitHub 查看全部
  • #52

    Bikeshed: gradle build jcenter() -> mavenCentral()

    更新于 2024年9月21日
  • #72

    gradlew run

    更新于 2024年9月20日
  • #71

    Project setup

    更新于 2023年11月7日
  • #68

    How to append the root directoory to the classpath

    更新于 2023年9月2日
  • #67

    strings/UsingStringBuilder.java has incorrect results

    更新于 2023年6月17日
  • #62

    Build failed when running `gradlew run`

    更新于 2023年1月13日
  • #66

    Gradle Build and available OpenJdk Releases

    更新于 2022年11月2日
  • #65

    I can't find the typeinfo package

    更新于 2022年10月11日
  • #61

    About Parallel Streams

    更新于 2022年6月30日
  • #60

    About Appendix: Understanding equals() and hashCode()

    更新于 2022年6月28日

核心特点

  • •Building From the Command Line: Quick Version
  • •Building From the Command Line: Detailed Instructions
  • •Install Java
  • •Macintosh
  • •Verify Your Installation
  • •Installing and Running the Book Examples
  • •Appendix A: Command-Line Basics
  • •The Shell
  • •Starting a Shell
  • •Directories

> 标签

Java

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言