- Modular Programming in Java 9
- Koushik Kothagal
- 534字
- 2021-07-02 18:38:21
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:
- Name the module: We've already done this! The name of our addressbook module is packt.addressbook.
- 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.
- 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!"); } }
- 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.
- Python機器學習:數據分析與評分卡建模(微課版)
- Java 開發從入門到精通(第2版)
- Cocos2d-x游戲開發:手把手教你Lua語言的編程方法
- JIRA 7 Administration Cookbook(Second Edition)
- Access 2010數據庫基礎與應用項目式教程(第3版)
- Visual Basic學習手冊
- Windows Forensics Cookbook
- Getting Started with Gulp
- 劍指Java:核心原理與應用實踐
- BeagleBone Black Cookbook
- Mastering Data Mining with Python:Find patterns hidden in your data
- IBM Cognos Business Intelligence 10.1 Dashboarding cookbook
- 新一代SDN:VMware NSX 網絡原理與實踐
- 匯編語言編程基礎:基于LoongArch
- Regression Analysis with Python