Migration Strategy: When to Use Module Path vs Classpath
You'll learn: Practical guidelines for deciding whether to adopt the module path or stick with the classpath in real-world Java applications.
Understanding the Decision Point
After learning how both classpath and module path work, you face a critical question: when should you actually migrate? The answer isn't always "immediately." The module path offers encapsulation, performance, and reliability benefits, but migration requires effort and changes to your build and deployment process.
When to Use Module Path
Choose the module path when:
- Starting new projects where you control all dependencies and can define clean module boundaries from day one
- You need strong encapsulation to protect internal APIs from misuse by other teams or future maintainers
- Performance matters and you want faster startup times through optimized class loading
- Preventing split packages is critical for maintaining a clean architecture
- Your dependencies are already modular (many modern libraries now ship as modular JARs)
When to Stay on Classpath
Stick with classpath when:
- Legacy codebases with complex dependency graphs that would require extensive refactoring
- Third-party libraries aren't modular and heavily use reflection without proper
opensdeclarations - Migration cost outweighs benefits for stable, working applications nearing end-of-life
- Build tools and frameworks in your stack don't yet fully support JPMS
- Team expertise is limited and training time isn't available
The Hybrid Approach
Remember: you can run modular JARs on the classpath and use the --module-path alongside --class-path. This lets you migrate incrementally, moving modules one at a time while keeping unmigrated code on the classpath.
Key Takeaway: Use module path for new projects and when strong encapsulation justifies migration effort; stick with classpath for stable legacy systems where the cost-benefit doesn't align, and consider hybrid approaches for gradual migration.