简体   繁体   English

无法使用 Ubuntu 运行 Java GUI 程序

[英]Unable to run Java GUI programs with Ubuntu

I am learning GUI in Java, and for that I have created a demo program:我正在用 Java 学习 GUI,为此我创建了一个演示程序:

import java.awt.*;

public class FrameDemo extends Frame {

    public FrameDemo(){
        super("Frame Demo");
        setSize(200, 200);
        setVisible(true);
    }

    public static void main(String args[]){
        new FrameDemo();    
    }
}

It was compiled successfully.它编译成功。 But when I tried to execute the program, I found the following error:但是当我尝试执行该程序时,我发现了以下错误:

Exception in thread "main" java.awt.HeadlessException
    at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:173)
    at java.awt.Window.<init>(Window.java:437)
    at java.awt.Frame.<init>(Frame.java:419)
    at FrameDemo.<init>(FrameDemo.java:4)
    at FrameDemo.main(FrameDemo.java:9)

I am using Xubuntu 10.10 and java -version gives:我正在使用 Xubuntu 10.10 和java -version给出:

java version "1.6.0_20"
OpenJDK Runtime Environment (IcedTea6 1.9.5) (6b20-1.9.5-0ubuntu1)
OpenJDK Client VM (build 19.0-b09, mixed mode, sharing)

What should I to do?我该怎么办?

One more thing: It is the same sort of error I got when I tried to execute Dr. Java and HJSplit's jar file.还有一件事:这与我尝试执行 Dr. Java 和 HJSplit 的 jar 文件时遇到的错误类型相同。

Ubuntu has the option to install a headless Java -- this means without graphics libraries. Ubuntu 可以选择安装无头 Java——这意味着没有图形库。 This wasn't always the case, but I encountered this while trying to run a Java text editor on 10.10 the other day.情况并非总是如此,但前几天我在 10.10 上尝试运行 Java 文本编辑器时遇到了这个问题。 Run the following command to install a JDK that has these libraries:运行以下命令以安装具有这些库的 JDK:

sudo apt-get install openjdk-6-jdk

EDIT: Actually, looking at my config, you might need the JRE.编辑:实际上,查看我的配置,您可能需要 JRE。 If that's the case, run:如果是这种情况,请运行:

sudo apt-get install openjdk-6-jre

I stopped getting this exception when I installed default-jdk using apt .当我使用apt安装default-jdk时,我不再收到此异常。 I'm running Ubuntu 14.04 (Trusty Tahr), and the problem appears to have been the result of having a "headless" Java installed.我正在运行 Ubuntu 14.04 (Trusty Tahr),问题似乎是安装了“无头”Java 的结果。 All I did was:我所做的只是:

sudo apt-get install default-jdk

In my case就我而言

-Djava.awt.headless=true

was set (indirectly by a Maven configuration).已设置(由Maven配置间接设置)。 I had to actively use我不得不积极使用

-Djava.awt.headless=false

to override this.覆盖这个。

I too had OpenJDK on my Ubuntu machine:我的 Ubuntu 机器上也有OpenJDK

$ java -version
java version "1.7.0_51"
OpenJDK Runtime Environment (IcedTea 2.4.4) (7u51-2.4.4-0ubuntu0.13.04.2)
OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)

Replacing OpenJDK with the HotSpot VM works fine:用 HotSpot VM 替换 OpenJDK 工作正常:

sudo apt-get autoremove openjdk-7-jre-headless

How to install the JDK on Ubuntu (Linux) 如何在 Ubuntu (Linux) 上安装 JDK

使用“env”命令检查您的 X Window 环境变量。

Use JFrame instead of Frame.使用 JFrame 而不是 Frame。 And do not extend from JFrame.并且不要从 JFrame 扩展。 Just write a class that has a JFrame property named gui , which configures this JFrame with the available methods, because it is better style doing it like this.只需编写一个具有名为gui的 JFrame 属性的类,该类使用可用方法配置此 JFrame,因为这样做更好。 Extending here is wrong the use of OOP.在这里扩展是错误的使用 OOP。

Check what your environment variable DISPLAY's value is.检查您的环境变量 DISPLAY 的值是什么。 Try running a simple X application from the command line.尝试从命令行运行一个简单的 X 应用程序。 If it works, check DISPLAY's value for the right value.如果有效,请检查 DISPLAY 的值以获得正确的值。

You can experiment with different values of and environment variable on a per invocation basis by doing the following on the command line:您可以通过在命令行上执行以下操作,在每次调用的基础上试验不同的 和 环境变量值:

DISPLAY=:0.0 <your-java-executable-here>

How are you calling your program?你怎么称呼你的程序?

I would check with another Java implementation/vendor.我会检查另一个 Java 实现/供应商。 Preferrably Oracle/Sun Java: http://www.java.com/en/ .最好是 Oracle/Sun Java: http : //www.java.com/en/ The open-source implementations unfortunately differ in weird ways.不幸的是,开源实现在奇怪的方面有所不同。

This command worked for me.这个命令对我有用。

Sudo dnf install java-1.8.0-openjdk (Fedora) Sudo dnf install java-1.8.0-openjdk (Fedora)

Sudo apt-get install java-1.8.0-openjdk

Should work for Ubuntu.应该适用于 Ubuntu。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM