Understanding Java applets
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 f开发者_StackOverflowiles? 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 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.
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.
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.
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.
精彩评论