File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes finalize an applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "finalize an applet" Watch "finalize an applet" New topic
Author

finalize an applet

Korol Bloom
Ranch Hand

Joined: Jan 13, 2004
Posts: 33
Hi,
I have a problem to unload my applet. I am testing like this: I start my applet by invoking the url that contains my applet. Then I leave and move to an other url, then back to my applet's url by hitting the browser's back-button.
The second time my applet is not loaded correctly. There is a NuPoExc, where none is at the first time it was started. It seems, that some classes or objects stay in the VM. Also The user is not getting asked to grant permissions again (i have a signed applet).
How can I go sure, that all my classes are unloaded? Is there any "golden method", that garbages all this?
Another idea: How can I get a list of all classes contained in my jar file? I cannot find a method at class "ClassLoader" or a related class, that provides me such list. If I would have that list, I could write a method like "dispose()" for all my classes, especially the singleton ones. From the list then I call "dispose()" on every class that implements it, and so I can set all references to null.
Or can I solve the problem by extending object's "finalize()"?
thanks for any suggestion
, korol
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
I think I would try overriding the stop() method in the applet to perform a destroy(). This would remove the applet from memory -- normally performed when the user exits the browser session.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Korol Bloom
Ranch Hand

Joined: Jan 13, 2004
Posts: 33
Thanks for sugestion. I think that calling "destroy()" won't change any behaviour, because it is an empty method by default. See the API:

public void destroy()

Called by the browser or applet viewer to inform this applet that it is being reclaimed and that it should destroy any resources that it has allocated. The stop method will always be called before destroy.
A subclass of Applet should override this method if it has any operation that it wants to perform before it is destroyed. For example, an applet with threads would use the init method to create the threads and the destroy method to kill them.
The implementation of this method provided by the Applet class does nothing.

Also I can see by println, that my browser calls "destroy()" as well when I am surfing to another url.
Any other ideas?
, korol
Korol Bloom
Ranch Hand

Joined: Jan 13, 2004
Posts: 33
Well, I did some test cases. Each case checked twice with the same result. The problem just occures, when I start my applet from the remote http server and do not grant permission. I remember there were some more exceptions (also from my code) last time I checked this problem, but I didn't save the exception tree. Maybe it is the best not to think about this problem now. It might disappear by itself.
There are three things, I don't understand:
1. Why is finalize() called, when I am coming back to the site? I thought, it should be called immediately after the destroy()-call, so at that time, when I am surfing away.
2. Why is finalize() called before applet initialization, when permission is granted, and after initialization, when I refused? By the way -- I still am prompted once for permission. When coming back to the site, the VM remembers my decision and just notifies me.
3. Why there is any difference between local and remote? It's exactly the same html and classes, and I am not accessing anything special during initialization, just reading from jar-files.
here my java-console printout:
-------------------------- from local filesystem, with privileges ------------------------
: applet initialized
: applet started
: stopping applet
: destroying applet
<surfing away, coming back>
finalize() called
: applet initialized
: applet started
-------------------------- from local filesystem, without privileges ------------------------
Information: No privileges granted. No file access.: [access denied (java.lang.RuntimePermission createSecurityManager)]
: applet initialized
: applet started
: stopping applet
: destroying applet
<surfing away, coming back>
Information: No privileges granted. No file access.: [access denied (java.lang.RuntimePermission createSecurityManager)]
: applet initialized
: applet started
finalize() called
------------------------ remote, with privileges ------------------------------
: applet initialized
: applet started
: stopping applet
: destroying applet
<surfing away, coming back>
finalize() called
: applet initialized
: applet started
------------------------ remote, without privileges ------------------------------
Information: No privileges granted. No file access.: [access denied (java.lang.RuntimePermission createSecurityManager)]
: applet initialized
: applet started
: stopping applet
: destroying applet
<surfing away, coming back>
Information: No privileges granted. No file access.: [access denied (java.lang.RuntimePermission createSecurityManager)]
java.lang.IllegalArgumentException: null source
at java.util.EventObject.<init>(EventObject.java:34)
at java.awt.AWTEvent.<init>(AWTEvent.java:225)
at java.awt.event.ComponentEvent.<init>(ComponentEvent.java:94)
at java.awt.event.WindowEvent.<init>(WindowEvent.java:174)
at java.awt.event.WindowEvent.<init>(WindowEvent.java:211)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:576)
at java.awt.Component.dispatchEventImpl(Component.java:3506)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.SentEvent.dispatch(SentEvent.java:50)
at java.awt.DefaultKeyboardFocusManager$DefaultKeyboardFocusManagerSentEvent.dispatch(DefaultKeyboardFocusManager.java:145)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:454)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
: applet initialized
: applet started
finalize() called
, korol
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: finalize an applet
 
Similar Threads
finalize method-- W Brogden's Mock
Question
finalize method
Destructor
Running applets as an application