java 同一个包的类可以分布在多个 Jar 文件中吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/708744/
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
Can classes of same package spread across multiple Jar files?
提问by Jon Skeet
I am using some classes from a JAR file and they belong to a package (com.abc.xyz).
我正在使用 JAR 文件中的一些类,它们属于一个包 (com.abc.xyz)。
The class am writing also belongs to that package but I won't be able to bundle my file into that JAR file. Is it possible to have classes that belong to the same package spread across multiple JAR files?
正在编写的类也属于该包,但我无法将我的文件捆绑到该 JAR 文件中。是否可以将属于同一个包的类分布在多个 JAR 文件中?
回答by Jon Skeet
By default, absolutely.
默认情况下,绝对。
However, if you wantto make sure that classes from a particular package are onlyloaded from one jar file, you can add that information to the manifest.
但是,如果您想确保来自特定包的类仅从一个 jar 文件加载,您可以将该信息添加到 manifest。
回答by Tom Hawtin - tackline
It's probably not something that you should want to do. If it's in the same package, should it not be packaged together (I believe Jigsaw intends to allow splitting packages between modules, but that's a different kettle of fish).
这可能不是您应该想做的事情。如果它在同一个包中,它不应该打包在一起(我相信 Jigsaw 打算允许在模块之间拆分包,但那是另一回事)。
It can be blocked if either package is marked sealed in the manifest. I would recommend marking whole jars as sealed as a matter of course.
如果任一包裹在清单中标记为密封,则可以阻止它。我建议理所当然地将整个罐子标记为密封。
It can also be blocked if there are different signers on the classes and the classes are loaded by the same class loader.
如果类上有不同的签名者并且这些类是由同一个类加载器加载的,也可能会被阻止。
If you load classes using a different class loader, although the "namespace" will be the same, you won't actually get package (and relevant part of protected) access.
如果您使用不同的类加载器加载类,尽管“命名空间”将相同,但您实际上不会获得包(和受保护的相关部分)访问权限。
回答by John Topley
I don't see why it wouldn't be possible. All that matters is that the classes are in the classpath.
我不明白为什么这不可能。重要的是这些类都在类路径中。
回答by John Topley
Sometimes you have to do that if you want to extend the functionality of third party libraries but they are not open sourcve and/or you don't have sources
有时,如果您想扩展第三方库的功能,但它们不是开源的和/或您没有源,则必须这样做

