How can I reduce an applet's loading time in a web page? I am using a 78K applet. It takes more than 20 sec. to load.
Make sure all your applet code is inside a JAR file - if it's not (i.e. its just in a bunch of class files) then browsers will generally open a new HTTP connection for each file (including for the class files of inner classes) - for small files, it generally takes longer to open the socket for the connection that it does to actually transmit the data. In a similar vein, make sure resources (sounds, images) that your applet uses are also in the JAR file.
78k is actually quite a lot for compiled Java code, so maybe the size of the classes comes from
other stuff that's in there - the biggest culprit of this is often strings stored in the classfile to facilitate linking. Some things you can do to
reduce this are:
- put all your code in one class (with no inner-classes): you lose elegance and data hiding, but all the strings necessary for internal linking will be removed.
- make sure everything (methods & fields) that can be private is.
- don't put your code into a package unless you really have to.
- look at your identifiers (class, method and field names) - can you make them shorter? There are some commercial tools which do this (for protected names), and some obfuscators do it too.
In the worst case, make your applet in two parts, with one very small one that starts immediately and displays "please wait" while it loads the other classes over the network. This isn't any faster, but it prevents the user thinking you've crashed.
One small note - most browsers are "lazy" about starting the JVM and classloading the basic runtime classes - they generally do this only when they encounter a page containing an applet for the first time. This means that when you first point the browser at a page containing java, that first applet will take longer to be displayed, as it has to wait until a few hundred class files are classloaded by the browser from disk. Thus you only get accurate times for loading applets over the network when you've already gone to some java-enabled pages and thus caused the JVM to start. BTW, you won't see this issue in HotJava, as it has (by virtue of its java nature) already started the JVM long before you start going to pages.