简体   繁体   中英

java.lang.AbstractMethodError - while using ANT Built jar

I'm getting java.lang.AbstractMethodError for a specific case (only when I create a jar of my classes with ant) and I can't get my head around it. Some help to understand this would be much appreciated.

Consider the following case (for some reason I've to keep the class hierarchy in my code as its below in example and I think doing so should not give me an AbstractMethodError in any case!):

 public interface Cat {
     void speak();
 }

 public interface ColoredHeadCat extends Cat {
     boolean isColoredHead();
 }

 public interface RedHeadCat extends ColoredHeadCat {
     boolean isRedHead();
 }

 public interface SuperRedCat extends RedHeadCat {
     boolean isSuperCat();
 }

 public abstract class AbstractCat {
     public void speak() {
         System.out.println("Meow!");
     }
 }

 public abstract class AbstractColoredHeadCat extends AbstractCat {
     // .. Not overriding speak() method here

     public boolean isColoredHead() {
         return true;
    }
 }

 public abstract class AbstractRedHeadCat extends AbstractColoredHeadCat {
     // .. Not overriding speak() method here

     public boolean isRedHead() { 
         return true;
     }
 }

 public class SuperRedHeadCat extends AbstractRedHeadCat implements SuperRedCat {
     // .. Not overriding speak() method here too

     public  boolean isSuperCat() {
         return true;
     }
 }

 public class Main {
     public static void main(String[] arrgh) {
         SuperRedHeadCat superCat = new SuperRedHeadCat();
         superCat.speak(); // No exceptions! Works like charm.

         Cat cat = new SuperRedHeadCat();
         cat.speak(); // Gives java.lang.AbstractMethodError! Why?
     }
 }

All classes are in the same package.

So the problem is I can't reproduce this through Eclipse: Everything runs perfectly. But after building a jar with ant , and then running 'Main' it throws AbstractMethodError . I'm out of ideas here about the reason for this behavior.

java -version

java version "1.6.0_27"
Java(TM) SE Runtime Environment (build 1.6.0_27-b07)
Java HotSpot(TM) 64-Bit Server VM (build 20.2-b06, mixed mode)

ant -version

Apache Ant version 1.8.0 compiled on February 1 2010


PS: Except the Main class, remaining classes are obfuscated during ant build.
This problem is as resolved if I let AbstractCat implement Cat interface , AbstractColoredHeadCat implement ColoredHeadCat interface and AbstractRedHeadCat implement RedHeadCat interface.

Which obfuscator are you using?

I tried with yguard and it works fine:

<taskdef name="obfuscate" classname="com.yworks.yguard.YGuardTask"  classpath="c:/Soft/Java/yguard-2.5.3/lib/yguard.jar" />
<target name="obf" depends="jar">
    <obfuscate >
        <property name="error-checking" value="pedantic"/>
        <inoutpair in="target/cat.jar" out="target/obf.jar"/>

        <shrink >
            <property name="error-checking" value="pedantic"/>
            <keep>
                <method name="void main(java.lang.String[])" class="Main"/>
            </keep>
        </shrink>

        <rename mainclass="Main">
            <expose >
                <class classes="public" methods="public" fields="public">
                    <patternset>
                        <include name="**.*"/>
                        <exclude name="Main"/>
                    </patternset>
                </class>
            </expose>
        </rename>
    </obfuscate>
</target>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM