← Back to Advanced Java Course | Chapter 4: Modern Java Features | Lesson 8 of 12

Java का Modules System

Module system Java code को नामी packages में group करता है जिनमें साफ़ नियम हैं कि वे क्या share करते हैं, जैसे labeled दरवाज़ों वाले कमरे किसी इमारत में। यह बड़े programs को organized रखता है।
Syntax
java
module module.name {
    requires other.module;
    exports package.name;
}

Introduction to Modules

Java Module System, जो मूल रूप से Project Jigsaw के अंतर्गत विकसित हुआ, आपको संबंधित packages को explicit, declared dependencies वाले एक single, reusable module में group करने देता है। यह किसी module को यह सीमित करने भी देता है कि उसके कौन से internal packages बाहरी दुनिया को दिखाई दें, पूरे classpath को scan करने से बचकर security और startup performance दोनों सुधारते हुए।

उदाहरण: Introduction to Modules

java
public class Main {
	public static void main(String[] args) {
		// A module groups packages with explicit, declared dependencies (see module-info.java)
		System.out.println("Module system groups related packages together");
	}
}

Module Descriptors

एक module अपने root पर रखी एक module-info.java file से define होता है, जो specify करता है कि module के कौन से packages वह दूसरे modules को उपयोग के लिए exports करता है, और बदले में compile और run करने के लिए किन दूसरे modules की उसे requires है।

उदाहरण: Module Descriptors

java
public class Main {
	public static void main(String[] args) {
		// module-info.java would declare:
		// module com.example.app {
		//     exports com.example.app.api;
		//     requires java.sql;
		// }
		System.out.println("module-info.java declares exports and requires");
	}
}

Loading Platform Modules

आप सभी currently loaded platform modules को list करने के लिए चल रही JVM से query कर सकते हैं, जो यह समझने का एक उपयोगी तरीका है कि कोई दिया गया application runtime पर JDK के किन हिस्सों तक असल में access रखता है।

उदाहरण: Loading Platform Modules

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a stream from the collection to process it step by step
		ModuleLayer.boot().modules().stream()
			.limit(3)
			.forEach(m -> System.out.println(m.getName()));
	}
}

Service Loader API

Module System मौजूदा ServiceLoader API के साथ सीधे integrate होता है, modules को explicitly यह declare करने देते हुए कि वे कौन सी services provide करते हैं और कौन सी consume करते हैं। यह modules को loosely coupled रखता है, क्योंकि consuming module को कभी अपनी उपयोग की जा रही service की concrete class जानने की ज़रूरत नहीं पड़ती।

उदाहरण: Service Loader API

java
import java.util.ServiceLoader;
public class Main {
	interface Greeter {
		String greet();
	}
	static class EnglishGreeter implements Greeter {
		public String greet() { return "Hello"; }
	}
	public static void main(String[] args) {
		Greeter g = new EnglishGreeter(); // ServiceLoader.load(Greeter.class) would discover providers declared in module-info.java
		System.out.println(g.greet());
	}
}

Module Layer API

आप किसी चल रही JVM के भीतर active module layers को inspect कर सकते हैं, जहां हर layer पहले से resolved modules के एक graph को उन्हें load करने के लिए ज़िम्मेदार class loaders के साथ represent करता है, जो ज़्यादातर plugin-style या modular application architectures बनाते समय relevant होता है।

उदाहरण: Module Layer API

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		ModuleLayer bootLayer = ModuleLayer.boot();
		// Print a line to the console
		System.out.println("Boot layer module count: " + bootLayer.modules().size());
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. module-info.java में किसी package को exports करना भूल जाना, जिससे दूसरे modules उसकी classes को नहीं देख पाते भले ही वे public हों।
  2. किसी matching requires line के बिना दूसरे module की classes का उपयोग करना, जो 'package is not visible' compile error देता है।
  3. यह उम्मीद करना कि reflection किसी module के private members तक पहुंच सकेगा, जबकि reflection का उपयोग करने वाले frameworks के लिए आपको package को opens करना चाहिए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.