Copies $ .class in jar files

2

When I compile a jar with maven, but when I unzip it, it generates me copies of several files with the symbol $.class , how can I omit this type of copies?

Example:

dao.class
dao$1.class
    
asked by Rubén Díaz 20.04.2018 в 17:26
source

1 answer

3

Imagine you have a class like

class A { ...}

That you keep in A.java . This generates when compiling a file A.class .

Now imagine that you also have an interface (or class) like this:

interface B {
   public void hola();
}

That you keep in B.java . This generates when compiling a file B.class .

And, to complete this example, imagine that in your class A you need an object that meets B , so you declare an anonymous class :

class A {
    private B b= new B() {
        public void hola() { };
    };
}

Your attribute B is a class that meets interface B, but it is not a class B, but an anonymous class (without a name), so the compiler saves it in A$1.class , because it is a class created within A.

If you delete that file, your program will stop working and launch a ClassNotFoundException .

    
answered by 20.04.2018 / 17:51
source