Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to import two jars with same packages?

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.

like image 992
user902383 Avatar asked Nov 29 '18 15:11

user902383


People also ask

Do I need to import if in same package?

If both classes are in same package, you don't have to import it.

Can I load a same class from two different JAR files in my one program?

1) In general: Yes you can have the same class in different . jar files: you just disambiguate them with a fully qualified package name.

Can two packages have same 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.

Can a jar include another jar?

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.


1 Answers

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.

like image 126
Naman Avatar answered Oct 22 '22 01:10

Naman