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

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.

主站蜘蛛池模板: 新河县| 额济纳旗| 商河县| 阿图什市| 邵武市| 鄂伦春自治旗| 北流市| 孟州市| 黑龙江省| 安平县| 民勤县| 四会市| 娱乐| 镇远县| 重庆市| 大埔县| 昌平区| 临海市| 长岛县| 南投市| 桐柏县| 久治县| 镇宁| 南溪县| 辽源市| 长宁县| 广平县| 灌云县| 苍溪县| 屏东县| 噶尔县| 陇西县| 靖边县| 井冈山市| 达州市| 彝良县| 吉木乃县| 东丽区| 澄江县| 砚山县| 来安县|