Preventing Others from Decompiling Your Java Files

Problem

But I don’t want people to be able to decompile my Java programs!

Solution

Obfuscate them.

Discussion

It has been said that for any weapon there is a defense, and for any defense there is a weapon. If the weapon is a decompiler, then the defense is something called an obfuscator . An obfuscator takes your program and tries to make it obscure, so that decompilation either will not work or will not be useful.

Because Mr. van Vliet, the late inventor of Mocha, did not release its source code, nobody else can take it over and maintain it, as we don’t have the source. Or do we? Of course! That’s it! We’ll just run it through itself. Well, if you can download a copy, you can try it. But what you’ll find is that it doesn’t work. The entire program has been obfuscated. Yes, Mr. van Vliet also wrote the first Java obfuscator, partly in reaction to all the people who flamed him on the Net for releasing Mocha. Due to his untimely death, his obfuscator is no longer available.

There are, of course, commercial obfuscation programs that will do some degree of obfuscation. Some of them actually encrypt the file and use a custom class loader to decrypt it at runtime. I suppose if you wanted to keep people from learning how your program worked, which you well might for commercial or other reasons, you’d want to use one of these tools. Again, a Java resource web site or a current Java developer’s magazine would be the place to go for the latest versions.

Get Java Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.