Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the use of the "requires" clause in a module-info file in Java 9?
A module is an important concept introduced in Java 9. By using this concept, we can able to divide code into smaller components called modules. Therefore, each module has its own responsibility and declare its dependency on other modules to work properly. In order to declare a module, we need to include the "module-info.java" file to root source code.
There are few types of "requires" clause in "module-info" file
1)
requires
<strong>module com.tutorialspoint.gui {
requires com.tutorialspoint.model;
requires java.desktop;
}</strong>
2) requires transitive
<strong>module com.tutorialspoint.model {
requires transitive com.core;
}</strong>
3) requires static
- mandatory at compilation: a compilation error can be raised if the module is not present in the path module at compilation.
- optional at runtime: the module can't be taken into account in the sanity check phase when an application is started. The application starts even if the module is not present.
For instance, we want to propose the persistence of the data of an application, either in an oracle database or h2database.
<strong>module com.tutorialspoint.model {
requires static ojdbc
requires static h2daabase.h2;
}</strong>
