Search code examples
javaobfuscationdecompiling

Decompiled Obfuscated Code "Cannot return from within an initializer"


I have a class file named StaticInitializer.class which obfuscated successfully. When I have decompiled it I got following result where IDE gives an compiler error "Cannot return from within an initializer". Removing "return" statement solves the problem. But In fact What I want to know is that how obfuscated class file works properly if it has an return statement in static initializer.

Is there a mismatch between compiling and running phase of java files, that obfuscation detects and uses them as a way of obfuscation?

Decompiled code

public class StaticInitializer {


    static {

        int a=12;
        int b=34;
        return ;
    }

}

Edited: Following operation shows that obfuscated class file is compiled with jdk 1.5

javap -c StaticInitializer.class
major version: 49  

J2SE 8 = 52, J2SE 7 = 51, J2SE 6.0 = 50,

J2SE 5.0 = 49,

JDK 1.4 = 48, JDK 1.3 = 47, JDK 1.2 = 46, JDK 1.1 = 45


Solution

  • A decompiler is never 100% accurate, as there are multiple ways of programming any line of code. ALso JAD (When we say decompiler for Java, it is almost synonymous to JAD) does not support JDK 1.5 and later. It is no longer developed or maintained.

    Also looking at the decompiled code its giving you error because,From Java 7 onwards, it looks for a main method before loading the class. This is a change from previous java versions and hence your static block is not executing. In previous versions, the behavior was that JRE used to look for main method post loading the class and after executing the static blocks. So it would work in previous versions but not in new versions of java. But since JAD is no longer maintained it might show you the obsolete code which was valid at once upon a time.