next up previous
Next: 6 Putting the Applet Up: Web Applets in Scala Previous: 4 Making The Jar


5 Using Proguard to Shrink the Jar

Proguard is a nice program that is used to shrink, obfuscate, optimize, and preverify Java code. We can use Proguard on Scala code as well, though it does not work perfectly. As you may have realized, the jar file we created contains a lot of code that we did not use: it contains the entire scala library, and I highly doubt one will write an applet that uses every library in Scala. So we need to remove all of these excess libraries using Proguard. Before running Proguard, we need to create a configuration file to tell Proguard what to do. Proguard has many options for modifying jar files, and their website is a great resource to learn about them (proguard.sourceforge.net.) For our purposes, we will keep things simple. Open a new text file named firstapplet.pro and place the follow in that file:

-injars			MyFirstApplet-raw.jar
-outjars		MyFirstApplet.jar
-libraryjars	<java.home>/lib/rt.jar

-keep public class MyFirstApplet.MainApplet

-dontwarn
-ignorewarnings
-keepattributes

The Proguard configuration file is perhaps the most complicated part of this process. First, note that extra whitespace between words and delimiters is ignored. I have lined up the file for readability. The first two lines of the file specify the input jar and the output jar, respectively. The next line, which begins with -libraryjars, is where you specify the jar file on which the applet depends. These jars will not be included in the resulting jar because they will be on the user's system as part of the Java Runtime Environment (JRE). <java.home> is a system property that will be automatically replaced with your Java directory. If this does not work for some reason, you can specify the full path of your Java directory (example: /usr/java/default/jre/lib/rt.jar). The next line of the Proguard configuration file tells Proguard to keep the main class of our applet. As your applet gets more complex, it may become necessary to explicitly keep other classes or methods from your code. For more information about this, see the Proguard website (proguard.sourceforge.net). The next two lines, -dontwarn and -ignorewarnings should probably seem a bit concerning. Warnings are usually helpful in diagnosing problems in code; however, because Proguard is not made to work explicitly with Scala, many warnings are generated when running Proguard on a Scala jar file. These warnings do not affect the jar, and we must ignore them in order for the jar to successfully compile. If you would like to see what the warning are and try to make sense of them, you can remove the -dontwarn line, which will show you the warnings but not prevent the jar from compiling. The final line of the Proguard configuration file, -keepattributes, tells Proguard to keep any optional attributes in the jar file. These attributes include Exceptions and InnerClasses, both of which are very important in Scala. You might be able to get away without keeping these attributes, but I do not recommend it.

Our raw jar file is compiled and our Proguard configuration file is ready, so now it is time to run Proguard on our jar file. Proguard is written in Java, so running it is quite easy. First, locate the Proguard directory. My Proguard directory is in my home directory, so I will use that as my example. To run Proguard on the raw jar file, run the following command:

java -jar ~/proguard4.5.1/lib/proguard.jar @firstapplet.pro

You may have a different version of Proguard, but that should not be a problem. Proguard will then execute the instructions in the configuration file (which we named firstapplet.pro). Once Proguard is finished, you should have a new file named MyFirstApplet.jar. Check the size of this file. It should be significantly smaller than our original jar file. At this time, feel free to remove the old jar file (MyFirstApplet-raw.jar).

After completing this step, we have all of the components necessary to put our applet on the web! The next section will briefly mention steps to take when putting these files on the web.


next up previous
Next: 6 Putting the Applet Up: Web Applets in Scala Previous: 4 Making The Jar
2008-08-28 2010-12-16