It’s depressing to read online just how easy it is for someone to decompile Java class file byte code, to turn it back into the original Java source code. You can’t prevent someone from decompiling your code, but you can make the decompiled code more difficult to understand. You can rename objects, variables, and method names to meaningless symbols; and rearrange and change code in such a way that it will still run the same way but is harder to understand. This process is called code obfuscation.

Two Reasons to Obfuscate Your Code

One reason to obfuscate program code is to protect your intellectual property. You don’t want someone else to decompile your code and use pieces or all of it as he wishes. Though you can’t prevent someone from decompiling your code, if he can’t understand the code, your coding secrets may remain secret. He may decide to not use any of your code in his own programs if he cannot figure out what the various parts of your code do.

Another benefit of obfuscating program code is to improve performance. How can obfuscation improve performance? First, most obfuscators remove unused classes, variables, and methods, thus reducing the size of the executable files. Many of the longer names you used for objects, variables, and methods are changed to smaller, meaningless names, so the executable file will be smaller. Smaller executable files load faster and consume less memory. Some obfuscators may also move code around in ways that improve performance.

Reasons to Not Obfuscate Your Code

A big disadvantage of obfuscated code is that it is normally very difficult to debug. This isn’t as much of a problem with Java because Java obfuscation is performed on the byte code, not the source code. The source code will still be untouched for debugging. However, if you receive a runtime error with a stack trace, it could be more difficult to locate what triggered the error, because the stack trace will give the identifier names and line numbers based on the obfuscated byte code.

As you decide whether to obfuscate your code, be aware of the limitations. At best, obfuscation merely makes it time-consuming, not impossible, to reverse engineer a program. Obfuscation makes your code a little more difficult for others to examine for security weaknesses. However, when security is important, you must use measures more effective than obfuscation.

Let a Program Do It

What’s the easiest way to systematically rename identifiers, move code around, and make it difficult to read? Let a program do it. ProGuard is another of my favorite free programs. ProGuard is an open source program that obfuscates Java program code and performs code optimization. It does all its work on the generated class files, never touching the source code.

You can download ProGuard from One of the many features I like about ProGuard is that it can be run as an Ant task, making code obfuscation an automated part of my build process.


If you obfuscate your code, it will be more difficult, but not impossible, for others to examine the coding techniques you used, and to copy your code. Obfuscating your code may also make your program perform better. However, obfuscating your code may make your program more difficult to debug. Whether or not you should obfuscate your code is something you must decide for yourself.

Related posts:

  1. How to Protect Your Software from Piracy
  2. What is Software Deployment?
  3. Should You Create a Windows .EXE Wrapper for Java Programs?
  4. Integrated Help and a User’s Guide for Your Programs
  5. Welcome to This Little Program Went to Market

About the Author

Comments are closed.

More Do-It-Yourself Java Games

More Do-It-Yourself Java Games: An Introduction to Java Graphics and Event-Driven Programming is the second book of the Do-It-Yourself Java Games series. You'll learn to create windows and dialogs, to add buttons and input fields, to use images and drawings, and to respond to keyboard input and mouse clicks and drags. You'll create 10 more games including several puzzles, a dice game, a word game, and a card game.

This book assumes you either have an understanding of basic Java programming or you have read the first book, Do-It-Yourself Java Games: An Introduction to Java Computer Programming. Read more.

Do-It-Yourself Java Games

Do-It-Yourself Java Games: An Introduction to Java Computer Programming uses a unique "discovery learning" approach to teach computer programming: learn Java programming techniques more by doing Java programming than by reading about them.

Through extensive use of fill-in blanks, with easy one-click access to answers, you will be guided to write complete programs yourself, starting with the first lesson. You'll create puzzle and game programs like Choose An Adventure, Secret Code, Hangman, Crazy Eights, and many more, and discover how, when, and why Java programs are written the way they are. Read more

Step-by-Step Tutorial

Many of the tips, techniques, and tools discussed in this blog are demonstrated in a detailed step-by-step tutorial in the book, This Little Program Went to Market, by Annette Godtland.

The book takes a computer program through the entire process of creating, deploying and distributing a program, then selling and marketing it (or any other product) on the Internet. Read more.