Java 在 Eclipse 中,modulepath 和 classpath 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50321602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Eclipse, what is the difference between modulepath and classpath?
提问by QuantumQuaver
In Eclipse, what is the difference between modulepath and classpath, and which one should I use to add a jar in the lib folder? And why does the JRE System Library appear in modulepath?
在Eclipse中,modulepath和classpath有什么区别,在lib文件夹中添加jar应该用哪个?以及为什么 JRE 系统库会出现在 modulepath 中?
采纳答案by Till Brychcy
The module system has mainly the following impact on the code:
模块系统对代码的影响主要有以下几点:
- A package can only be accessed from one module (Nested packages are treated as separate, so even though the package
java.util
is in the modulejava.base
, the packagejava.util.logging
can be in the modulejava.logging
) - You can only access public fields and methods of code in exported packages of other modules. This is true even with reflection (i.e.
java.lang.reflect.AccessibleObject.setAccessible(boolean)
only works for code in the same module)
- 一个包只能从一个模块访问(嵌套包被视为独立的,所以即使包
java.util
在模块中java.base
,包java.util.logging
也可以在模块中java.logging
) - 您只能访问其他模块的导出包中的公共字段和代码方法。即使使用反射也是如此(即
java.lang.reflect.AccessibleObject.setAccessible(boolean)
仅适用于同一模块中的代码)
All code that is on the classpath lives together in the "unnamed" module. All code on the modulepath lives in their own "named" modules.
类路径上的所有代码都存在于“未命名”模块中。模块路径上的所有代码都位于它们自己的“命名”模块中。
You have to distinguish two cases:
您必须区分两种情况:
If you don't add a module-info.java to your project, your project will be part of the unnamed module and can see all other code in the unnamed module, plus code in
java.base
and code in modules injava.se
root module. Basically this means that w.r.t. code on the classpath, everything still works as in Java 8, so you should just put your dependencies on the classpath.If you have a module-info.java in your project, your project will be in its own named module and can only see code in
java.base
and other named modules which are references using "requires"-clauses in the module-info.java. As named modules are only found via the module path, you should put your dependencies on the classpath. This even works for jars created before java 9, which will get a module name derived from the .jar file name (in which case they are called "automatic" module).
如果您没有在项目中添加 module-info.java,您的项目将成为未命名模块的一部分,并且可以看到未命名模块中的所有其他代码,以及根模块中的代码输入
java.base
和模块中的代码java.se
。基本上这意味着在类路径上编写代码,一切仍然像在 Java 8 中一样工作,因此您应该将依赖项放在类路径上。如果你的项目中有一个 module-info.java,你的项目将在它自己的命名模块中,并且只能看到在
java.base
module-info.java 中使用“requires”子句引用的代码和其他命名模块。由于命名模块只能通过模块路径找到,因此您应该将依赖项放在类路径上。这甚至适用于在 java 9 之前创建的 jar,它将获得从 .jar 文件名派生的模块名称(在这种情况下,它们被称为“自动”模块)。
The JRE is always on the module-path, so that its internal code cannot be accessed even from code on the classpath.
JRE 始终位于模块路径上,因此即使从类路径上的代码也无法访问其内部代码。
There is one special case: If you have a module-info.java in your project and have test code in your project, you usually don't want to mention test dependencies like junit in the module-info.java
. There are two solutions for this:
有一种特殊情况:如果你的项目中有一个 module-info.java 并且你的项目中有测试代码,你通常不想在module-info.java
. 对此有两种解决方案:
Create a dedicated test module. This has always been the convention for osgi-based projects. Disadvantage is that you can only use public api in your tests
The solution used by maven: Put your test dependencies on the classpath. When compiling test code, maven adds command line options that allow the code in the named module to read the unnamed module (which is not possible via the module-info.java).
创建专用测试模块。这一直是基于 osgi 的项目的惯例。缺点是您只能在测试中使用公共 api
maven 使用的解决方案:将你的测试依赖放在类路径上。编译测试代码时,maven 添加命令行选项,允许命名模块中的代码读取未命名模块(通过 module-info.java 无法实现)。
In Eclipse Oxygen, the maven solution was not possible, because it has no notion which code is test code, but this has been implemented in the upcoming Eclipse Photon (4.8) release, which will be out in June. You can already work with the (feature-complete) milestone builds from http://download.eclipse.org/eclipse/downloads/. In case you find any bugs, please report them at https://bugs.eclipse.org/bugs/.
在 Eclipse Oxygen 中,maven 解决方案是不可能的,因为它不知道哪些代码是测试代码,但这已经在即将发布的 Eclipse Photon (4.8) 版本中实现,该版本将于 6 月发布。您已经可以使用来自http://download.eclipse.org/eclipse/downloads/的(功能完整的)里程碑版本。如果您发现任何错误,请在https://bugs.eclipse.org/bugs/报告它们。