How does Eclipse run .java files as applets?

十年热恋 提交于 2019-12-20 01:10:35

问题


I've been trying to run a simple applet that I created from the command line. I tried just doing:

C:\java Applet 

It obviously didn't work; however, I noticed that Eclipse lets me run the applet if I select the class and select run as java applet. How does Eclipse do this?


回答1:


I believe IDEs typically launch applets using the appletviewer, but using an unrestricted security policy (the appletviewer when launched from the command line is sand-boxed).

To launch the applet viewer from the command line, try this:

  • In the source, add a comment at the top along the lines of a standard applet element.
  • Run the applet viewer using the command.

    prompt> appletviewer TheApplet.java

    Note particularly that rather than provide HTML as the argument to the applet viewer (the old way), applet viewer will now parse the applet element directly out of the source.

See also the applet info. page for an example of using the applet element at the top of the source: E.G.

/* <!-- Defines the applet element used by the appletviewer. -->
<applet code='HelloWorld' width='200' height='100'></applet> */
import javax.swing.*;

/** An 'Hello World' Swing based applet.

To compile and launch:
prompt> javac HelloWorld.java
prompt> appletviewer HelloWorld.java  */
public class HelloWorld extends JApplet {

    public void init() {
        // Swing operations need to be performed on the EDT.
        // The Runnable/invokeLater() ensures that happens.
        Runnable r = new Runnable() {
            public void run() {
                // the crux of this simple applet
                getContentPane().add( new JLabel("Hello World!") );
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

When launched from the command line, the applet viewer will have an even more restrictive sand-box than is applied to an applet deployed in a browser.


An alternative to appletviewer is Appleteer which I can highly recommend, because it is better than appletviewer ( and I wrote it ;).




回答2:


you need to use appletviewer command from the command line to run an Applet




回答3:


Applet is a graphic component that extends Panel and has Applet context. It is not a big problem to emulate this environment. You just have to create application with main method that creates frame with border layout and puts applet in the center of this frame. This application should also implement applet context that provides API to retrieve parameters.

As was mentioned by @Satya JDK has such utility named appletvieer that can be executed from command line. Eclipse can either use appletvieer directly or implement is own viewer as described above.



来源:https://stackoverflow.com/questions/10440043/how-does-eclipse-run-java-files-as-applets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!