next up previous
Next: 5 Using Proguard to Up: Web Applets in Scala Previous: 3 The HTML File


4 Making The Jar

We are now ready to compile our source code into a jar file that can be run on the web; however, it is not as simple as compiling just our source code into the jar. We must also compile the Scala libraries into the jar file. Unlike Java, Scala is not very widely distributed, and while the Java libraries are on every computer that has the Java Virtual Machine (JVM), most people will not have the Scala libraries. So in order for our Scala code to run on the user's machine, we must include the Scala libraries in our jar.

The first step is to copy the necessary Scala jar file into our working directory. Find your Scala directory (varies by system) and go into the lib directory. Copy scala-library.jar and scala-swing.jar to your working directory. Next, we need extract these jar files. To do this, use the following commands:

jar xf scala-library.jar
jar xf scala-swing.jar

Note: If the Java directory is not in your search path, your shell will not be able to find the jar command. First, find the appropriate directory in which the jar command is located (/usr/java/default/bin/ on my system) and either add this directory to your search path or run the command using the full path name (for example: /usr/java/default/bin/jar xf scala-library.jar).

It may take some time to extract these files. Once you are finished extracting these two jar files, feel free to remove the scala-library.jar and scala-swing.jar files (only delete the copies in your working directory, not the files in your main Scala directory). You can also delete some of the other files that were generated from the jar extraction process, including the META-INF directory and the library.properties file, but do not delete the scala directory that was created from extracting the jars. This directory contains the .class files for the Scala libraries. We will compile these files into our applet jar.

The next step is to compile our Scala code. Although you probably know how to do this, I will still cover how, just in case. To compile your scala code, either use the scalac command or, as I prefer, the fsc command:

fsc Main.scala

If the code does not compile successfully, then fix the errors and compile again. Your later applications may have multiple Scala files. Make sure to compile all of them successfully before moving on to the next step. Once you have successfully compiled your code, its time to compile your code and the Scala libraries into a jar file. Use the following command to create a jar containing your source code and the Scala libraries:

jar cf "MyFirstApplet-raw.jar" MyFirstApplet scala

A bit about this command. MyFirstApplet-raw.jar is the name of the jar that will be created. MyFirstApplet is the package name of your Scala code. scala is the name of the folder in which the scala libraries reside, so when you repeat this step for future projects, this will not change but the other two names will. You may notice that the jar name has a strange ending to it: -raw is part of the jar's name. The reason for this is that this jar will not be the one we put out on the web. Take a look at the size of this jar. On my system, this jar is 6.8 megabytes! If we were to use this jar for our applet, it would take quite a while for the applet to load. So how do we get a more reasonable sized jar file? In the next section, we will use Proguard to shrink this jar.


next up previous
Next: 5 Using Proguard to Up: Web Applets in Scala Previous: 3 The HTML File
2008-08-28 2010-12-16