The same class file in multiple .jar files. How bad is that?

I have a library that writes data in text or binary format. It has the following three components:

  • general data structures
  • text writer (depends on 1)
  • binary writer (depends on 1)

The obvious way to distribute this file is to 3.jar the file, so users can include only what they need.

However, the “common data structures” component is actually just two small classes, so I’m considering creating only two .jar files and including common .class files in both.

My question is: What are the potential problems with this?

+4
source share
4 answers

A potential version that is inconsistent with the others is actually one of the cases of a large set of loading problems that may arise when you deploy the same class in different banks.

Class loading errors will most likely bite you in an application container / EJB container or similar installation, where there are several components / applications loaded by a hierarchy of class loaders. However , if the same class is loaded by two different class loaders, they are perceived by the JVM as completely different classes! . This can lead to various runtime errors, such as LinkageError (for example, if two different versions of the same class definition collide - as described in other answers), ClassCastException (if a throw attempt is made between two class definitions loaded by different class loaders) and etc. Believe me, the addon descriptor is a place you do not want to see.

I would put the whole library in one jar to minimize this risk.

+4
source

If you have released a new version of your library, a user who uses both libraries (one old, new) may receive runtime exceptions when the new library receives a class from 1 from the old library (or vice versa if it is not backward compatible). The easiest way is to free everything in one bank so that you do not receive this version.

+1
source

Potential problems are that in future versions of the data structure between these two jar files may be inconsistent (say, because there is a bug fix or a minor version). In this case, you can get a ClassDefNotFoundException if you need to include both applications in the application. I would recommend either splitting it into three jar files, or one large one.

0
source

I recommend using JarJar , which is a library for packing multiple jar files in a single jar file.

There is an Ant task to integrate into your assembly, and so your build environment may just contain raw jars, and you may just have a simple deployment (but remember to include license.txt files from various libraries with your distribution).

0
source

Source: https://habr.com/ru/post/1307856/


All Articles