I'm migrating project from Java 8 to Java 11, and modularity introduced in java 9 is giving me a headache. Couple jars have same packages, but different classes. This is giving me error
module SomeModule reads package my.package from both ModuleA and ModuleB
I wonder how to deal with cases like that, given that not all jars are under my control, refactoring is not a solution.
If both classes are in same package, you don't have to import it.
1) In general: Yes you can have the same class in different . jar files: you just disambiguate them with a fully qualified package name.
Yes, you can have two classes with the same name in multiple packages. However, you can't import both classes in the same file using two import statements.
Adding jars to a single jar is done using the jar command. Suppose you have jarA, jarB and jarC. For your deployment you would need a manifest file too. The manifest would specify the external jars' full path.
One of the way is to make sure both these jars end up on the classpath
in The Unnamed Module. But do take a note that
The unnamed module exports all of its packages. This enables flexible migration... It does not, however, mean that code in a named module can access types in the unnamed module.
A named module cannot, in fact, even declare a dependence upon the unnamed module. This restriction is intentional, since allowing named modules to depend upon the arbitrary content of the class path would make reliable configuration impossible.
Ofcourse the ideal solution would be to do a bottom up migration and ensure that no two modules expose the same package to the module that requires
both of them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With