简体   繁体   English

在 Eclipse 中,modulepath 和 classpath 有什么区别?

[英]In Eclipse, what is the difference between modulepath and classpath?

In Eclipse, what is the difference between modulepath and classpath, and which one should I use to add a JAR file in the lib folder?在 Eclipse 中,modulepath 和 classpath 有什么区别,在lib文件夹中添加 JAR 文件应该使用哪个?

And why does the JRE System Library appear in modulepath?以及为什么 JRE 系统库会出现在 modulepath 中?

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 module java.base , the package java.util.logging can be in the module java.logging )一个包只能从一个模块访问(嵌套包被视为独立的,所以即使包java.util在模块java.base ,包java.util.logging也可以在模块java.logging
  • You can only access public fields and methods of code in exported packages of other modules.您只能访问其他模块的导出包中的公共字段和代码方法。 This is true even with reflection (ie java.lang.reflect.AccessibleObject.setAccessible(boolean) only works for code in the same module)即使使用反射也是如此(即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 in java.se root module.如果你没有在你的项目中添加 module-info.java,你的项目将成为未命名模块的一部分,并且可以看到未命名模块中的所有其他代码,加上java.base代码和java.base中模块中的java.se根模块。 Basically this means that wrt code on the classpath, everything still works as in Java 8, so you should just put your dependencies on the classpath.基本上这意味着在类路径上编写代码,一切仍然像在 Java 8 中一样工作,因此您应该将依赖项放在类路径上。

  • 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.如果你的项目中有一个 module-info.java,你的项目将在它自己的命名模块中,并且只能看到java.base和其他命名模块中的代码, java.base模块是在模块信息中使用“requires”子句的引用。爪哇。 As named modules are only found via the module path, you should put your dependencies on the module path.由于命名模块只能通过模块路径找到,因此您应该将依赖项放在模块路径上。 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).这甚至适用于在 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 .有一种特殊情况:如果您的项目中有一个 module-info.java 并且您的项目中有测试代码,您通常不想在module-info.java提及测试依赖项,例如 junit 。 There are two solutions for this:对此有两种解决方案:

  • Create a dedicated test module.创建专用测试模块。 This has always been the convention for osgi-based projects.这一直是基于 osgi 的项目的惯例。 Disadvantage is that you can only use public API in your tests缺点是您只能在测试中使用公共 API

  • The solution used by Maven : Put your test dependencies on the classpath. Maven使用的解决方案:将您的测试依赖项放在类路径上。 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).在编译测试代码时,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.在 Eclipse Oxygen 中,Maven 解决方案是不可能的,因为它不知道哪些代码是测试代码,但这已经在即将发布的Eclipse Photon (4.8) 版本中实现,该版本将于 6 月发布。 You can already work with the (feature-complete) milestone builds from http://download.eclipse.org/eclipse/downloads/ .您已经可以使用来自http://download.eclipse.org/eclipse/downloads/的(功能完整的)里程碑版本。 In case you find any bugs, please report them at https://bugs.eclipse.org/bugs/ .如果您发现任何错误,请在https://bugs.eclipse.org/bugs/报告它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM