Book Image

Modular Programming in Java 9

By : Koushik Srinivas Kothagal
Book Image

Modular Programming in Java 9

By: Koushik Srinivas Kothagal

Overview of this book

The Java 9 module system is an important addition to the language that affects the way we design, write, and organize code and libraries in Java. It provides a new way to achieve maintainable code by the encapsulation of Java types, as well as a way to write better libraries that have clear interfaces. Effectively using the module system requires an understanding of how modules work and what the best practices of creating modules are. This book will give you step-by-step instructions to create new modules as well as migrate code from earlier versions of Java to the Java 9 module system. You'll be working on a fully modular sample application and add features to it as you learn about Java modules. You'll learn how to create module definitions, setup inter-module dependencies, and use the built-in modules from the modular JDK. You will also learn about module resolution and how to use jlink to generate custom runtime images. We will end our journey by taking a look at the road ahead. You will learn some powerful best practices that will help you as you start building modular applications. You will also learn how to upgrade an existing Java 8 codebase to Java 9, handle issues with libraries, and how to test Java 9 applications.
Table of Contents (19 chapters)
Title Page
Credits
About the Author
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface

Preface

Modularity is coming to Java with Java 9, and it's a big deal! Unlike other Java releases that come with added features to the language that you can optionally use in your code, the Java Platform Modular System is a complete change in the way we think about, design, and write Java applications.

This book is a detailed guide and a hands-on companion to help you learn about and write modular code in Java. After reading and working through the code in this book, you'll have a deep understanding of Java 9 modularity, its features, the impact on the platform, and how you can use this new paradigm to build modular applications yourself.

This book has been carefully designed to provide a gentle introduction to the topics, while gradually ramping up the scope and complexity of the topics covered. This is not a reference book. For example, when I introduce a concept or a feature, I don't provide all possible details or syntax combinations in an attempt to be comprehensive. My primary goal while writing this book is to help you grasp the concepts and gain a deep understanding of Java modularity. The ordering of topics and discussions in this book has been carefully crafted to make sure you always have the necessary knowledge to understand the topic being covered. Thus, this book benefits greatly from being read sequentially. Having said that, if you are already aware of some of the topics and you want to jump to a specific topic, you should be able to do that too. A handy index is provided at the end of the book for this purpose.

What this book covers

Chapter 1, Introducing Java 9 Modularity, covers the status quo of code structuring and management in Java 8 and before. It outlines the challenges and pitfalls in maintaining and organizing large code bases over time, with the example of a large Java code base everyone is familiar with--the JDK. It then introduces Project Jigsaw and explains how the concept of modularity attempts to solve the challenges outlined in the previous chapter. It discusses the module-based changes to the JDK structure as well as the ability to create modules in application code.

Chapter 2, Creating Your First Java Module, gets you started on a sample code project that is written without using the module construct. It then guides you step-by-step through creating your first Java 9 module. It introduces you to the module keyword, the module-info.java class and how to define a custom Java module. It then covers compiling and running a module. It explains the structure of the .class files and how the runtime uses the same module definition to infer the module structure.

Chapter 3, Handling Inter-Module Dependencies, shows how modules can hardly work well in isolation. They are designed to be parts of a bigger unit and are meant to work with each other. This chapter covers creating a second module and having the modules depend on each other. You'll learn how to define such a relationship in Java 9, as well as how to compile and run a multi-module application.

Chapter 4, Introducing the Modular JDK, switches our attention to the Java platform, specifically the JDK. You'll learn about the important changes to the JDK that has resulted from the modularization of the platform. You'll learn about the modules that come out of the box with the JDK. You'll also learn about the tools and techniques to browse and get more information about any of those modules.

Chapter 5, Using Platform APIs, teaches you how to use the platform modules in the sample application through a hands-on step-by-step guide. You'll understand some of the challenges that you may typically face when depending on platform modules and how to solve them.

Chapter 6, Module Resolution, Readability, and Accessibility, delves deep into inter-module dependencies and how you can control the level of encapsulation for their modules and libraries. It continues the module resolution discussion of the previous chapter by adding two new criteria that influences the encapsulation and availability of Java elements--accessibility and readability.

Chapter 7, Introducing Services, looks at a vital element of coupling that exists between two modules in the sample application and how that prevents the extensibility and "plugging-in" of new modules. It explains the new Java 9 services, provides step-by-step instructions to encapsulate implementation classes, and uses ServiceLoader to look them up.

Chapter 8, Understanding Linking and Using jlink, introduces the static linking step in the Java 9 development process and what happens during it. It explains how linking plays an important part when developing modules in Java 9. It then provides step-by-step instructions to create a runtime image for the sample project, how to optimize it, and how to execute the image.

Chapter 9, Module Design Patterns and Strategies, covers several best practices when it comes to building modular applications in Java. Now that you have a good understanding of the Java module system features and how it works, the next question is when and how to use them? You'll learn how to establish module scopes and boundaries, how to define good module interfaces, and how to tackle some common challenges when building a modular application.

Chapter 10, Preparing Your Code for Java 9, walks you through getting an old sample code base (written in Java 7) ready to be migrated to Java 9. It illustrates the optional nature of Java modules and how a classpath-based code is automatically assigned to an "unnamed" module. It then provides step-by-step instructions to get legacy code to compile and run in Java 9. It also shows you how to handle issues with usage of encapsulated types, and how to work around them.

Chapter 11, Migrating Your Code to Java 9, walks you through upgrading legacy code to use the new modularity features of Java 9. You'll learn how to form a migration strategy for your code, as well as how to deal with dependencies that are not compatible with Java 9. You'll learn how to use Java 9 features that are designed to assist such migration, such as automatic modules and command-line overrides.

Chapter 12, Using Build Tools and Testing Java Modules, covers two important aspects of Java programming--build tool integration and unit testing. You'll learn to use Maven to structure your projects and align Maven's multi-module project concepts with Java 9 modular applications. You'll also learn how to test Java modules using JUnit.

What you need for this book

To follow the example code in this book, you'll need a computer running a reasonably recent version of Windows, macOS, or Linux. You'll also need a text editor to edit the code. I highly recommend using a text editor that allows you to open multiple files at a time and switch between them easily.

Who this book is for

If you are a Java developer who has been coding Java applications, and you want to learn about the new modularity features in Java 9, you are the perfect audience for this book. You may working on a Java 9 modular project, or may be tasked with the effort to migrate an existing Java code base to Java 9, or you may have found the buzz around modularity in Java intriguing and want to learn more; either way, this is the right book for you!

This book assumes you are familiar with the Java programming language and have written some Java code before. It also assumes you are comfortable with the command-line interface in your operating system of choice. The commands you need to run will be provided in the book.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We'll first call the getContacts() method on an instance of ContactUtil to get the hardcoded Contact list."

A block of code is set as follows:

    module packt.addressbook { 
      requires packt.sortutil; 
    }

Any command-line input or output is written as follows. Lines beginning with indicate an input command. The input command might be broken into several lines to aid readability, but needs to be entered as one continuous line at prompt:

$ export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Click on File | New Project, you'll see a New Project overlay with a new option in the Java category--Java Modular Project."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book--what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply email [email protected], and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your email address and password.
  2. Hover the mouse pointer on the SUPPORT tab at the top.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box.
  5. Select the book for which you're looking to download the code files.
  6. Choose from the drop-down menu where you purchased this book from.
  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows
  • Zipeg / iZip / UnRarX for macOS
  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/koushikkothagal/Modular-Programming-in-Java-9. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books--maybe a mistake in the text or the code--we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.