Search code examples
javastrip.class-filedebug-information

Any Java counterpart for `/usr/bin/strip`?


Is there any tool that can remove debug info from Java .class files, just like /usr/bin/strip can from C/C++ object files on Linux?

EDIT: I liked both Thilo's and Peter Mmm's answers: Peter's was short and to the point exposing my ignorance of what ships with JDK; Thilo's ProGuard suggestion is something I'll definitely be checking out anyway for all those extra features it appears to provide. Thank you Thilo and Peter!


Solution

  • ProGuard (which the Android SDK for example ships with to reduce code size), can do all kinds of manipulation to shrink JAR files:

    • Evaluate constant expressions.
    • Remove unnecessary field accesses and method calls.
    • Remove unnecessary branches.
    • Remove unnecessary comparisons and instanceof tests.
    • Remove unused code blocks.
    • Merge identical code blocks.
    • Reduce variable allocation.
    • Remove write-only fields and unused method parameters.
    • Inline constant fields, method parameters, and return values.
    • Inline methods that are short or only called once.
    • Simplify tail recursion calls.
    • Merge classes and interfaces.
    • Make methods private, static, and final when possible.
    • Make classes static and final when possible.
    • Replace interfaces that have single implementations.
    • Perform over 200 peephole optimizations, like replacing ...*2 by ...<<1.
    • Optionally remove logging code.

    They do not mention removing debug info in that list, but I guess they can also do that.

    Update: Yes, indeed:

    By default, compiled bytecode still contains a lot of debugging information: source file names, line numbers, field names, method names, argument names, variable names, etc. This information makes it straightforward to decompile the bytecode and reverse-engineer entire programs. Sometimes, this is not desirable. Obfuscators such as ProGuard can remove the debugging information and replace all names by meaningless character sequences, making it much harder to reverse-engineer the code. It further compacts the code as a bonus. The program remains functionally equivalent, except for the class names, method names, and line numbers given in exception stack traces.