views:

46

answers:

4

Hi,

I am trying to understand how applets work.

I understand that a class file is bytecode, something that a JVM can understand and execute. When an applet runs on the user's system, the bytecode is transferred to the user system over the network.

Now, what happens when there are more than one class files? For example, what if the class that has the applet's init() method (class A) has an object of another of my classes (Class B)? The html file still contains classA.class in the APPLET tag. In such a scenario, does the classB.class also gets transferred over the network?

Also, how do jar files fit in here? They are just a compressed collection of class files, isn't it?

Any clarity about how this works would be greatly appreciated.

Thanks,

A: 

I think the key fact from what you describe above is that the jar files get transferred and interpreted by the JVM before the applet runs and therefore knows and therefore can access the entire class hierarchy.

James
So, 'what goes on the wire'? All the class files required? And jar is just a way of compressing them, so that we consume less of the bandwidth?
Chaitanya
A jar reduces latency by using only one network round trip-as opposed to n trips (one for each file)
MeBigFatGuy
+1  A: 

A jar file is the file that the classes are contained in, it uses zip-compression. When the browser finds a tag referring to an applet it downloads the jar file onto the client and runs it.

Nathan Hughes
Thanks for the reply.
Chaitanya
+1  A: 

Any dependencies that your applet relies on will be transferred to the client. This includes both individual classes and jar files.

However, many java developers now prefer using Java Web Start instead of applets. Web Start applications don't run inside the browser, which helps alleviate compatibility problems with different browsers' Java plugins. Furthermore, the Web Start sandbox is not as restrictive as the sandbox that applets run inside. If you have already written your code in Applet form, it can still be launched as a Web Start application.

dbyrne
Thanks a lot for the reply. I remember coming across Web Start.. will definitely explore it; i need a non-restrictive sandbox!
Chaitanya
A: 

The <APPLET> tag is deprecated in HTML; the <OBJECT> tag is supposed to be prefered. However, Sun (Oracle?) says that <APPLET> is to be preferred as <OBJECT> support is spotty. Take that for what it's worth.

JAR files (in the context of <APPLET>) are specified with the archive parameter.

I would expect that all files are fetched from the base of the URL that the applet was in; archives are certainly.

I'd check out the Java Tutorials from Sun. They have one on applets, including launching via Java Web Start.

David