Home >Java >javaTutorial >How to use module path to control access to classes in Java 9

How to use module path to control access to classes in Java 9

WBOY
WBOYOriginal
2023-07-31 23:27:20736browse

How to use module paths to control class access in Java 9

With the release of Java 9, a new module system has been introduced that can be used to control and manage access to code. This new feature is called module paths and helps us better organize and protect our code. This article will introduce how to use module paths to control access permissions of classes in Java 9, and provide corresponding code examples.

Before Java 9, Java program classes and packages were managed and accessed through classpath. However, this approach can easily lead to increased coupling between classes and is not flexible enough. Using module paths can solve these problems and provide better code organization and management capabilities.

First, we need to create a Java module that contains the module-info.java file. A module is a collection of related classes and packages, and a set of rules for exporting and referencing.

Let's assume we have a simple project with two modules: main and common. Our goal is to access a class in the common module in the main module.

First, we need to create the common module. Create the module-info.java file in the root directory of the common module with the following content:

module common {
    exports com.example.commonpackage;
}

In the above code, we use the exports keyword to export the com.example.commonpackage package in the common module. This means that other modules can access and use these classes and packages in the common module.

Next, we create the main module. Similarly, create the module-info.java file in the root directory of the main module with the following content:

module main {
    requires common;
}

In the above code, we use the requires keyword to reference the common module. This means that the main module needs to depend on the common module in order to be able to access and use the classes and packages in the common module.

Now, we can use the classes in the common module in the code of the main module. Suppose there is a class named CommonClass in the common module, we can use it in the code of the main module. Here is an example:

package com.example.mainpackage;

import com.example.commonpackage.CommonClass;

public class MainClass {
    public static void main(String[] args) {
        CommonClass commonClass = new CommonClass();
        commonClass.doSomething();
    }
}

Note that we need to use the import statement to import the CommonClass class in the common module.

Finally, we need to use Java 9’s command line compiler to compile our code. Suppose we place the common and main modules in the common and main folders respectively, we can use the following command to compile:

javac -d out --module-source-path common/main -m main

In the above command, the -d parameter specifies the compilation output folder, --module The -source-path parameter specifies the source code path of the module, and the -m parameter specifies the module to be compiled.

After successful compilation, we can use the following command to run our program:

java --module-path out --module main/com.example.mainpackage.MainClass

In the above command, the --module-path parameter specifies the module path, and the --module parameter specifies the module to be run. Modules and main classes.

By using module paths, we can better control and manage access to our code. We can ensure that only authorized modules can access and use specific classes and packages, thereby improving code security and maintainability.

To summarize, this article introduces how to use module paths to control class access in Java 9. We manage code access by creating modules and using the module-info.java file to define export and reference rules. In this way, we can better organize, protect and manage the code, improving the maintainability and security of the code.

I hope this article will help you understand and use the module path of Java 9.

The above is the detailed content of How to use module path to control access to classes in Java 9. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn