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

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.

主站蜘蛛池模板: 商丘市| 礼泉县| 万全县| 邵东县| 上蔡县| 怀集县| 崇义县| 九江县| 抚州市| 怀宁县| 山东省| 大理市| 德昌县| 潜江市| 晋城| 独山县| 文昌市| 资兴市| 南康市| 瑞昌市| 丰台区| 上林县| 龙井市| 娱乐| 定安县| 应用必备| 蓬莱市| 个旧市| 金华市| 盈江县| 石家庄市| 拜城县| 高尔夫| 德清县| 富平县| 万年县| 秦安县| 沙河市| 临漳县| 明水县| 上饶市|