官术网_书友最值得收藏!

Creating your first Java module

Let's start by creating our first Java 9 module and walk through the process of coding, compiling, and executing a module. Ideally, any application should consist of multiple modules, but in this chapter, we'll start small. In this chapter, we'll create just one module called packt.addressbook. This will be our first Java module. Over the next few chapters, we'll break this down into multiple modules.

We'll obviously need to start with the folder where all our code resides. In the screenshots in this book, I've chosen the path <home>/code/java9, but you can feel free to use any path of your preference. I'll be referring to this folder as the project root folder throughout this book.

We've just learned the four steps required to create any Java module. Let's run through those four steps for the addressbook module now:

  1. Name the module: We've already done this! The name of our addressbook module is packt.addressbook.
  1. Create a module root folder: You'll now need to create one module folder for each module you intend to write. Since we are creating only one module called packt.addressbook, we create a folder with the same name. I recommend keeping all your Java source files in a separate folder called src in the project root folder. You'll then create all your module root folders in the src folder. Since my project root is ~/code/java9, the module root folder resides at ~/code/java9/src/packt.addressbook. This packt.addressbook folder is where all the packages and Java files of the module reside.
  2. Add code to the module: This step is business as usual for Java developers. From the module root folder onward, your folder structure reflects your package. For our first attempt, let's write a simple Hello World application. Create a Java file called Main.java in the package packt.addressbook. The complete path for Main.java is ~/code/java9/src/packt.addressbook/packt/addressbook/Main.java. Let's add a main method that just prints a message to the console:
        package packt.addressbook; 
        public class Main { 
          public static void main(String[] args) { 
            System.out.println("Hello World!"); 
          } 
        } 
Note that the actual difference from the previous Java 8 directory structure is the introduction of the module root folder. It's helpful to think about each module as a subproject of sorts. The package structure begins from the module root folder onward.
  1. Create a module descriptor: Create a file called module-info.java and place it directly in the module root folder. We'll go over the details of what can be configured in this file in Chapter 3, Handling Inter-Module Dependencies, but for now, create this file with the following contents:
        module packt.addressbook { 
 
        }

The keyword module is followed by the name of the module, which in our case is packt.addressbook. There's nothing between the curly braces for now because we are not specifying any details about the module in this file yet, except for the module name. However, adding this file to the module directory is essential for the Java compiler to treat this as a module.

With this, you are done! These four steps are what it takes to create a simple Java 9 module. Here's how the folder structure should look when you are done:

Now, let's move on to compiling and executing this module.

主站蜘蛛池模板: 绿春县| 汉寿县| 南江县| 上林县| 淄博市| 肇庆市| 齐河县| 肇源县| 福海县| 葵青区| 林周县| 遂溪县| 秦皇岛市| 调兵山市| 商都县| 永登县| 阜平县| 河北省| 嵩明县| 宝应县| 会理县| 西青区| 竹溪县| 东乡县| 佛山市| 沁水县| 唐海县| 杂多县| 股票| 保德县| 遂平县| 华蓥市| 栾城县| 视频| 江口县| 长泰县| 卫辉市| 长沙市| 汾阳市| 英吉沙县| 柘城县|