6

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!

Harry
  • 3,684
  • 6
  • 39
  • 48
  • 3
    **javac -g:none** disables debug info at all at compile time. And first google hit is this http://blogs.sun.com/ahe/entry/how_to_strip_debug_information – PeterMmm Mar 10 '11 at 10:00
  • 2
    The blog Peter linked to points to `pack200 -r -G debug.jar`. Pack200 ships with the JDK. – Thilo Mar 10 '11 at 10:06
  • 1
    strip does not remove debugging information - it removes entries from the symbol table which are not required. If you removed all the symbols from a jar file it wouldn't work. – symcbean Mar 10 '11 at 12:31
  • Archive of the blog post from comment #1: https://web.archive.org/web/20070821053326/http://blogs.sun.com/ahe/entry/how_to_strip_debug_information – cachius Jul 01 '23 at 16:48

1 Answers1

4

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.

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • 1
    Hey, that was pretty awesome. Will check it out, thanks! (Here's a +1 right away). Meanwhile, outside of what comes by default with Oracle JDK, is there any comprehensive list of *all* system-level tools for `javac` and `java` (JVM) for things like... speed/memory benchmarking and tuning, monitoring, static/dynamic code analysis, etc. Basically, I don't even know what all is possible with Java (other than regular, high-level programming) and what all kinds of tools are at my disposal to do all of what!! – Harry Mar 10 '11 at 10:08
  • 1
    There is the official docs: http://download.oracle.com/javase/6/docs/technotes/tools/index.html – Thilo Mar 10 '11 at 10:15
  • Also, there are many many third-party tools. The easiest way I've found so far to find them is to ask on SO… – Donal Fellows Mar 10 '11 at 11:07