Running a Modular Application with java
What you'll learn: How to launch your compiled modular application using the java command with module-specific flags.
The Core Idea
Once you've compiled your module using javac, you need to tell the java launcher where to find your modules and which module contains your application's entry point. This is different from the traditional classpath approach—modules live on the module path, and you specify the starting module and main class using the --module flag.
The Essential Flags
You need two key pieces of information:
--module-path(or-p): Specifies where to find your compiled modules (the directory containing module folders or JAR files)--module(or-m): Identifies which module to run and its main class, in the formatmodule.name/fully.qualified.MainClass
The Launch Command
Here's the typical syntax:
java --module-path out --module com.example.myapp/com.example.myapp.Main
Or using the shorter flags:
java -p out -m com.example.myapp/com.example.myapp.Main
What Happens Behind the Scenes
Think of --module-path as telling Java, "Look in this directory for modules," while --module says, "Start the application by running this specific class inside this module." The module system resolves all dependencies declared in module-info.java before launching your application, ensuring everything needed is present.
If you've packaged your module as a JAR with a Main-Class attribute in the manifest, you can omit the class name:
java -p out -m com.example.myapp
Key Takeaway: Use java --module-path <location> --module <module.name>/<main.class> to launch modular applications, replacing the traditional classpath with the module path for better encapsulation and dependency management.