Eclipse 如何将 .java 文件作为小程序运行? [英] How does Eclipse run .java files as applets?

查看:25
本文介绍了Eclipse 如何将 .java 文件作为小程序运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试运行我从命令行创建的简单小程序.我试着做:

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

C:\java Applet 

显然没有用;但是,我注意到如果我选择类并选择作为 java 小程序运行,Eclipse 可以让我运行小程序.Eclipse 如何做到这一点?

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?

推荐答案

我相信 IDE 通常使用 appletviewer 启动小程序,但使用不受限制的安全策略(从命令行启动时的 appletviewer 是沙盒的).

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:

  • 在源代码中,沿着标准小程序元素的行在顶部添加注释.
  • 使用命令运行小程序查看器.

    <代码>提示>appletviewer TheApplet.java

    特别要注意的是,applet 查看器现在将直接从源文件中解析applet 元素,而不是将HTML 作为参数提供给applet 查看器(旧方式).

另见小程序信息.页面中使用源顶部小程序元素的示例:E.G.

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.

appletviewer 的替代品是 Appleteer,我强烈推荐它,因为它比 appletviewer(和我写的 ;).

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

这篇关于Eclipse 如何将 .java 文件作为小程序运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆