tags:

views:

66

answers:

4

I am trying to wrap my head around the rules of applet use. I realize that an applet does not have to have a main method (it can use init() instead) but every code sample my professor provides is an applet with a main method, like in the code below.

My understanding was that it is not a good idea to have a main method in an applet because of security issues, is this correct? When (if ever) should I use a main method in an applet?

Also, my professor is using applets like this embedded into a PowerPoint presentation which he can then run easily during his presentation. Would it still be possible to run an applet like this which was embedded into a PowerPoint presentation if the applet used inti() instead of main()? I ask that because I cannot see any reason why he would use a main method in such an applet unless it was required for the applet to run properly when embedded into a PowerPoint presentation.

//EventDispatcherThreadDemo.java

import javax.swing.*;

public class EventDispatcherThreadDemo extends Japplet{

    public EventDispatcherThreadDemo(){
        add(new JLabel("Hi, it runs from an event dispatch thread"));
    }

    public static void main(Stirng[] args){

        SwingUtilities.invokeLater(new Runnable(){

            public void run(){
                JFrame frame = new JFrame("EventDispatcherThreadDemo");
                frame.add(new EventDispatcherThreadDemo());
                frame.setSize(200, 200);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });

    }

}

P.S. This has nothing to do with homework, just trying to learn more about applets.

+4  A: 

The main method does not have any special meaning to an applet.

Your professor is including it along with a few lines to fire up the class in a JFrame, to be able to run it from the command line too with a simple "java foobar" command. This as opposed to having to run the "appletviewer foobar" command which may have issues with being launched from another program like PowerPoint. it does not give the full Applet environment though, but the demonstration programs may be so simple, it doesn't matter.

Why he chose to do so instead of just presenting you with a big HTML page with the applets in, I do not know.

Thorbjørn Ravn Andersen
A reason for doing this is that it is often easier to run a pure Java program in a debugger than try an applet
Mark
I just have not messed with applets that much, and I am afraid I will soon have to make one. I have no problem making a regular Java application, so I guess if I can continue to use what I know (using the main method) I should be ok. It almost seems like I can just `extend Applet` or `extend JApplet` in my regular application and BOOM, I have an applet. Is pretty much correct?
typoknig
Not really. See the Applet section of the official Java Tutorial - http://download.oracle.com/javase/tutorial/deployment/applet/
Thorbjørn Ravn Andersen
+3  A: 

The main() has no security implications for an applet since it is not called by an applet container. Even if it was, the applet would still be confined to a security sandbox.

OTOH, developing a hybrid applet/application can make a lot of sense. Traditionally Frame based apps. were easier to develop and debug than applets.

It can also make sense if you want to offer both forms to the end user. ;)

See this example of a hybrid that might help demonstrate.

Andrew Thompson
+1  A: 

Actually there is at least one security issue. Suppose your applet:

  1. Is signed
  2. Has debugging code left in its main method that does something potentially dangerous like write to a file

Then an attacker can create a Java Web Start application descriptor that points to your .jar file and launches your main method with full privileges.

This specific attack can be defended against by adding an empty file called JNLP‑INF/APPLICATION.JNLP to your .jar file. But there may be other possible attacks, so I think your professor is right to avoid it.

finnw
Well, yes, but the issue here is that you have been signing a jar file with dangerous code in there.
Thilo
What would be the point of signing a jar if it didn't have dangerous code?
Tom Hawtin - tackline
@Thilo, the point is that you expect your applet's `init` method to be called and you will probably audit it before signing the jar. But if you mistakenly assume your `main` method is unreachable when deployed as an applet then you may neglect to check it.
finnw
+2  A: 

Unfortunately lots of example code takes short cuts which should not be followed. The danger here is that the learner is not in a good position what to imitate and what not to.

Applet life-cycle method and the main method are example of upcalls. Like listeners they should be short, extract necessary information, encapsulate handling of the upcall and call a method on an object that is meaningful to that object (in particular not encumbered by dependency on the upcall).

Tom Hawtin - tackline