Computer Science as profession and hobby, has about ten years of Java experience. Firm believer of software engineering and lover of agile methodologies. Always interest on GIS related technologies, and conceretely, in data visualization since I began working with weather radar data. Skills in Java, HTML, JavaScript, CSS, Databases PHP, and.. always positive. Antonio is a DZone MVB and is not an employee of DZone and has posted 26 posts at DZone. You can read more from them at their website. View Full User Profile

Obfuscating a NetBeans Java Application Project

  • submit to reddit

Some time ago I found a couple of posts talking about how to obfuscate a NetBeans RCP module (here and here).

Getting some parts of the ant targets presented in the previous post, this one presents a simple target that allows to obfuscate a normal Java library.

For this, you need to have installed the obfuscator ProGuard.

Take into account I am talking about obfuscating a Java library. This implies the obfuscation is lighter than if you obfuscate a closed application, that is, all public methods and interfaces must maintain its name (if not you can call your library methods anymore).

Open your build.xml Java application file and paste this target:

    <target name="-post-jar">
<property name="proguard.jar.path" value="/path/to/proguard.jar" />
<property name="java.home.path" value="/path/to/java/home" />

<taskdef resource="proguard/ant/"
classpath="${proguard.jar.path}" />

<echo message="Obfuscating ${dist.jar}..."/>
<mkdir dir="${build.dir}/obfuscated"/>
<proguard printmapping="${build.dir}/obfuscated/${application.title}.map"
renamesourcefileattribute="SourceFile" ignorewarnings="true">

<!-- Specify the input jars, output jars, and library jars. -->
<injar file="${dist.jar}" />
<outjar file="${build.dir}/obfuscated/BalloonWindCore_JavaSE.jar" />

<libraryjar path="${javac.classpath}" />
<libraryjar file="${java.home.path}/jre/lib/rt.jar" />

<!-- Keep some useful attributes. -->

<keepattribute name="InnerClasses" />
<keepattribute name="SourceFile" />
<keepattribute name="LineNumberTable" />
<keepattribute name="Deprecated" />
<keepattribute name="*Annotation*" />
<keepattribute name="Signature" />

<!-- Preserve all public classes, and their public and protected fields and methods. -->

<keep access="public">
<field access="public protected" />
<method access="public protected" />

<!-- Preserve all .class method names. -->

<keepclassmembernames access="public">
<method type ="java.lang.Class"
name ="class$"
parameters="java.lang.String" />
<method type ="java.lang.Class"
name ="class$"
parameters="java.lang.String,boolean" />

<!-- Preserve all native method names and the names of their classes. -->

<method access="native" />

<!-- Preserve the methods that are required in all enumeration classes. -->

<keepclassmembers extends="java.lang.Enum">
<method access="public static"
parameters="" />
<method access="public static"
parameters="java.lang.String" />

<!-- Explicitly preserve all serialization members. The Serializable
interface is only a marker interface, so it wouldn't save them.
You can comment this out if your library doesn't use serialization.
With this code serializable classes will be backward compatible -->

<keepnames implements=""/>
<keepclassmembers implements="">
<field access ="final"
type ="long"
name ="serialVersionUID" />
<field access ="!static !transient"
name ="**"/>
<field access ="!private"
name ="**"/>
<method access ="!private"
name ="**"/>
<method access ="private"
type ="void"
name ="writeObject"
parameters="" />
<method access ="private"
type ="void"
name ="readObject"
parameters="" />
<method type ="java.lang.Object"
name ="writeReplace"
parameters="" />
<method type ="java.lang.Object"
name ="readResolve"
parameters="" />

<!-- Your application may contain more items that need to be preserved;
typically classes that are dynamically created using Class.forName -->


Special attention to these couple of lines:

        <property name="proguard.jar.path" value="/path/to/proguard.jar" />
<property name="java.home.path" value="/path/to/java/home" />


Your rating: None Average: 5 (2 votes)
Published at DZone with permission of Antonio Santiago, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Paolo Lizarazu replied on Thu, 2010/04/29 - 11:36am

hola he intentando poner esta tarea pero no me funciona aparentemente necesito un archivo de propiedades

si podriasponer este tambien o talves no es necesario te lo agrradeceria

<taskdef resource="proguard/ant/"
             classpath="${proguard.jar.path}" />

me sale este error 

Reading library jar [C:\Program Files\Java\jdk1.6.0_18\jre\jre\lib\rt.jar]
D:\MobiusWS\PrinterEquipaje\build.xml:84: Can't read [proguard.ClassPathEntry@f3c3cea] (No such file or directory)
GENERACIÓN INCORRECTA (total time: 1 second)

Matt Coleman replied on Wed, 2012/06/06 - 12:50am in response to: Paolo Lizarazu

Gracias!!i now understand this graphic design buffalo

Mateo Gomez replied on Thu, 2012/06/07 - 3:55am

obfuscating was a good decision mexican salsa recipes


Memnune Aycan replied on Tue, 2012/08/07 - 7:00am

I prepared a project with netbeans7.1 and java. I want to use proguard to obustacate.

I copied your build.xml and pasted in my project folder but when I opened project and selected build I got the massege D:\sql\Obus\build.xml:2: Unexpected element "{}target" {}target Obus is the name my project.Special attention to these  lines:<target name="-post-jar">
<property name="proguard.jar.path" value="C:/tool/proguard4.8/lib/proguard.jar" />
<property name="java.home.path" value="C:/Program Files/Java/jdk1.7.0_03/bin" /> 

Thank you in advance. 


Memnune Aycan


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.