在Eclipse中,modulepath与classpath的区别及JAR配置、JRE路径疑问
Nice question—this is a super common point of confusion once you start working with Java 9+ modules in Eclipse. Let's break it down step by step:
modulepath vs classpath: Core Differences
These two serve similar purposes (telling the JVM where to find code), but they work fundamentally differently:
- Classpath: The classic approach from pre-Java 9 days. It's a flat space for non-modular JARs (no
module-info.javafile) and loose class files. The JVM looks up classes here by their fully qualified names, which can lead to class conflicts (e.g., two JARs with the same class name). It's designed for the old "everything is in one global namespace" model. - Modulepath: Introduced in Java 9 with the Java Platform Module System (JPMS). It's for modular JARs (those with a
module-info.javadefining the module's name, dependencies, and exported packages) and the JDK's own modules. Each module has its own isolated namespace, so class conflicts are avoided. Dependencies are explicit—you have to declare which modules your code needs viarequiresinmodule-info.java, making project structure clearer and more maintainable.
Adding JARs from the lib folder: Which one to choose?
It depends on whether your project is modular or not:
- Non-modular projects (no
module-info.java): Stick to adding JARs to the classpath. This is the traditional setup, fully compatible with Java 8 and earlier, and works seamlessly for legacy libraries that don't have module metadata. - Modular projects (with
module-info.java):- If the JAR is a modular JAR (has its own
module-info.java): Add it to the modulepath, then declare the dependency in your project'smodule-info.javawithrequires [module-name];. - If the JAR is non-modular: You can add it to the modulepath (it becomes an "automatic module"—the JVM generates a module name from the JAR filename, usually stripping version numbers) or the classpath (it becomes part of the "unnamed module"). Adding to modulepath is generally better for modular projects, as it keeps dependency management consistent with the JPMS model.
- If the JAR is a modular JAR (has its own
Why does JRE System Library show up in modulepath?
Java 9 modularized the entire JRE. Instead of a single monolithic runtime, the JDK is split into dozens of modules (like java.base, java.sql, java.desktop). Each module contains a specific set of APIs, and they depend on each other explicitly.
Eclipse puts the JRE System Library in the modulepath because:
- For modular projects, you can declare exactly which JRE modules your code needs (e.g.,
requires java.sql;if you're working with databases) instead of pulling in the entire JRE. - Even for non-modular projects, the JVM handles the transition seamlessly—your code (in the unnamed module) can access classes from JRE modules without explicit declarations, thanks to JPMS compatibility rules.
Hope that clears up all your questions! If you run into specific issues setting this up in Eclipse, feel free to follow up.
内容的提问来源于stack exchange,提问作者Invariance

