简体   繁体   中英

Can classes of same package spread across multiple Jar files?

I am using some classes from a JAR file and they belong to a package (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?

By default, absolutely.

However, if you want to make sure that classes from a particular package are only loaded from one jar file, you can add that information to the manifest .

除非JAR 密封了包装,否则这是非常可行的。

I don't see why it wouldn't be possible. All that matters is that the classes are in the classpath.

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

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.

有时你必须这样做,如果你想扩展第三方库的功能,但它们不是开放的和/或你没有来源

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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