在Linux终端中打开一个小程序

时间:2017-03-15 10:46:10

标签: java linux applet

我的要求是在Linux终端中打开一个小程序。

我创建了一个java类和一个名为A.java和A.html的html文件,如下所示:

A.java

public class A extends Applet
{
    private int w, h;
    public void init( )
    {
            System.out.println(" in init");
            w = 45;
            h = 50;
    }

    public void paint(Graphics g)
    {
            g.drawRect(w, h, 20, 80);
    }
}

A.html

     <html>
        <p> This file launches the 'A' applet: A.class! </p>
        <applet code="A.class" height=200 width=320>
      </applet>
     </html>

当我尝试执行它时,我遇到了这些错误:

[root@VMS e42]# appletviewer A.java
[root@VMS e42]# appletviewer A.html
Exception in thread "main" java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program       performed          an            operation which requires it.
        at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:204)
    at java.awt.Window.<init>(Window.java:536)
    at java.awt.Frame.<init>(Frame.java:420)
    at java.awt.Frame.<init>(Frame.java:385)
    at sun.applet.AppletViewer.<init>(AppletViewer.java:159)
    at sun.applet.StdAppletViewerFactory.createAppletViewer       (AppletViewer.java:98)
    at sun.applet.AppletViewer.parse(AppletViewer.java:1158)
    at sun.applet.AppletViewer.parse(AppletViewer.java:1092)
    at sun.applet.Main.run(Main.java:156)
    at sun.applet.Main.main(Main.java:98)

我需要改变什么?

1 个答案:

答案 0 :(得分:0)

错误消息不言而喻:您需要提供X11服务器(并设置您的DISPLAY环境变量以指向它)。小程序是AWT类,需要图形显示;你不能在普通终端上运行一个。

顺便说一句,以root身份运行是个坏主意;以root身份运行X11应用程序更糟糕。