19

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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • BTW, can you describe why you need to do so? It is a good practice to keep the entire package inside a single jar. It would be interesting to see why you can't. – Bartosz Klimek Apr 02 '09 at 08:03
  • I had to do that (to be able to access,and subclass classes with "default" access in a Jar that wasn't mine), and it worked well... – Varkhan Apr 02 '09 at 17:35

5 Answers5

18

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
8

It is quite doable unless the JAR has sealed the package.

gustafc
  • 28,465
  • 7
  • 73
  • 99
4

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

John Topley
  • 113,588
  • 46
  • 195
  • 237
4

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.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
0

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