Reason for IncompatibleClassChangeError

2

Recently I noticed some error messages in my code, the message that appears is this:

  

Exception in thread "Thread-3" java.lang.IncompatibleClassChangeError

The strange thing is that it only happens with jar ready and on other machines, on the machine where I generated jar works normally.

What is the reason for this exception and how can I prevent it from happening?

    
asked by anonymous 10.10.2017 / 21:06

1 answer

3

An error of type IncompatibleClassChangeError is usually an improper compilation process.

For example:

  • Create the following classes:

    public class Teste {
        public static void main(String[] args) {
            Teste2.hello();
        }
    }
    
    public class Teste2 {
        public static void hello() {
            System.out.println("Oi");
        }
    }
    
  • Compile the classes like this:

    javac Teste.java Teste2.java
    
  • Change the Teste2 class by removing the static modifier:

    public class Teste2 {
        public void hello() {
            System.out.println("Oi");
        }
    }
    
  • Recompile only the class Teste2 :

    javac Teste2.java
    
  • Run the program:

    java Teste
    
  • Here's the output:

    Exception in thread "main" java.lang.IncompatibleClassChangeError: Expected static method Teste2.hello()V
            at Teste.main(Teste.java:3)
    

    There are several other ways to generate IncompatibleClassChangeError , but all of them are the result of mixing classes that have been altered and recompiled in ways that are incompatible with each other. The best suggestion to fix this is to delete any .class files from your project and recompile everything from scratch.

        
    10.10.2017 / 21:19