If the developed j2s application is directly packaged into a jar file for release, it may be decompiled by others. It is not difficult to decompile class files. To protect our program code from being cracked, we can use the proguard obfuscator. Fortunately, eclipse has integrated proguard.
Proguard is an open-source software written based on the Java language. Therefore, it needs the Java2 runtime environment. We can download it from http://Proguard.sourceforge.net for free. The latest version is proguard3.0. Decompress the package and install it in C:/proguard3.0.1. Run eclipse, select the menu windows-preferences-j2me-obfuscation, where we should specify the correct proguard root directory, because we need to retain the extended MIDlet class when obfuscation, otherwise the program will not be able to execute. Therefore, public class * extends javax. microedition. MIDlet. MIDlet should be written in proguard keep expressions. See
Next we will write code, compile, and pre-verify as well as the applications for the development of J2EE. To save time, skip these steps here. The following code is used as an example to describe how to use proguard to confuse the j2s application:
Import javax. microedition. MIDlet. MIDlet;
Import javax. microedition. MIDlet. midletstatechangeexception;
Import javax. microedition. lcdui .*;
Import java. Io .*;
Import javax. microedition. Io .*;
Public class imagegetter extends MIDlet implements commandlistener
{
Private display;
Public static final command conncommand = new command ("Connect ",
Command. Item, 1 );
Public static final command exitcommand = new command ("exit", command. Exit,
1 );
Private form mainform;
Private getterthread gt;
Protected void Startapp () throws midletstatechangeexception
{
Display = display. getdisplay (this );
Mainform = new form ("image Getter ");
Mainform. append ("Click Connect to get image ");
Mainform. addcommand (conncommand );
Mainform. addcommand (exitcommand );
Mainform. setcommandlistener (this );
Display. setcurrent (mainform );
GT = new getterthread (this );
GT. Start ();
}
Public void setimage (image)
{
Mainform. append (image );
Display. setcurrent (mainform );
}
Protected void pauseapp ()
{
}
Protected void destroyapp (Boolean arg0) throws midletstatechangeexception
{
}
Public void commandaction (command cmd, displayable disp)
{
If (cmd = conncommand)
{
Synchronized (this)
{
Notify ();
}
} Else if (cmd = exitcommand)
{
Exitmidlet ();
}
}
Private void exitmidlet ()
{
Try
{
Destroyapp (false );
Yydestroyed ();
} Catch (midletstatechangeexception E)
{
E. printstacktrace ();
}
}
Class getterthread extends thread
{
Private imagegetter MIDlet;
Public static final string url = "http: // localhost/j2medev.png ";
Private httpconnection httpconn = NULL;
Private inputstream is = NULL;
Public getterthread (imagegetter MIDlet)
{
This. MIDlet = MIDlet;
}
Public void run ()
{
Synchronized (MIDlet)
{
Try
{
MIDlet. Wait ();
} Catch (interruptedexception E)
{
E. printstacktrace ();
}
}
System. Out. println ("connect to server ...");
Try
{
Httpconn = (httpconnection) connector. Open (URL );
Is = httpconn. openinputstream ();
Bytearrayoutputstream baos = new bytearrayoutputstream ();
Int CH = 0;
While (CH = is. Read ())! =-1)
{
Baos. Write (CH );
}
Byte [] imagedata = baos. tobytearray ();
Image image = image. createimage (imagedata, 0, imagedata. Length );
MIDlet. setimage (image );
Baos. Close ();
Is. Close ();
Httpconn. Close ();
} Catch (ioexception E)
{
E. printstacktrace ();
}
}
}
}
Right-click the project GetImage-J2ME-create obfuscated package so that proguard will confuse class files other than the MIDlet. As shown in:
In the deploy folder, we can see the getimage. jar, getimage. you can use the jar command to unbind getimage from Jad and other files generated by proguard. jar, and found that another class file has been obfuscated into. class, the MIDlet class is not changed. In addition to the obfuscation function, proguard will reduce our jar files and improve the running efficiency.