在Eclipse中,modulepath和classpath之间有什么区别? [英] In Eclipse, what is the difference between modulepath and classpath?

查看:6273
本文介绍了在Eclipse中,modulepath和classpath之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Eclipse中,modulepath和classpath之间的区别是什么,我应该使用哪一个在lib文件夹中添加jar?为什么JRE系统库出现在模块路径中?

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?

推荐答案

模块系统对代码的影响主要是:

The module system has mainly the following impact on the code:


  • 只能从一个模块访问一个包(嵌套包被视为单独的,所以即使包 java。 util 在模块 java.base 中,包 java.util.logging 可以在模块中 java.logging

  • 您只能访问其他模块的导出包中的公共字段和代码方法。即使使用反射也是如此(即 java.lang.reflect.AccessibleObject.setAccessible(boolean)仅适用于同一模块中的代码)

  • 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)
  • 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)

类路径中的所有代码都在未命名模块中共存。
模块路径上的所有代码都存在于他们自己的命名模块中。

All code that is on the classpath lives together in the "unnamed" module. All code on the modulepath lives in their own "named" modules.

你必须区分两种情况:


  • 如果你没有将module-info.java添加到项目中,你的项目将成为未命名模块的一部分,并且可以看到未命名模块中的所有其他代码,加上 java.base 中的代码和 java.se 根模块中的模块中的代码。基本上这意味着w.r.t.在类路径上的代码,一切仍然像Java 8一样工作,所以你应该把你的依赖项放在类路径上。

  • 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. 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.

如果你有一个模块信息。在你的项目中,你的项目将在你自己的命名模块中,并且只能看到 java.base 中的代码和其他使用requires-clauses的引用的命名模块module-info.java。由于命名模块只能通过模块路径找到,因此您应该将依赖项放在类路径上。这甚至适用于在java 9之前创建的jar,它将获得从.jar文件名派生的模块名称(在这种情况下,它们被称为自动模块)。

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).

JRE始终位于模块路径上,因此即使从类路径上的代码也无法访问其内部代码。

The JRE is always on the module-path, so that its internal code cannot be accessed even from code on the classpath.

有一个特例:如果你的项目中有一个module-info.java并且你的项目中有测试代码,你通常不想在模块中提到像junit这样的测试依赖项-info.java 。有两种解决方案:

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:


  • 创建专用测试模块。这一直是基于osgi的项目的惯例。缺点是您只能在测试中使用公共API

  • 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

maven使用的解决方案:将测试依赖项放在类路径上。在编译测试代码时,maven添加了命令行选项,允许命名模块中的代码读取未命名的模块(这是通过module-info.java无法实现的)。

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).

在Eclipse Oxygen中,maven解决方案是不可能的,因为它没有概念代码是测试代码,但是这已经在即将发布的Eclipse Photon(4.8)版本中实现,该版本将于6月发布。您已经可以使用 http://download.eclipse.org/上的(功能完备的)里程碑版本蚀/下载/ 。如果您发现任何错误,请通过 https://bugs.eclipse.org/bugs/。

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中,modulepath和classpath之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆