问题
I am learning GUI in Java, and for that I have created a demo program:
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:
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.
回答1:
Ubuntu has the option to install a headless Java -- this means without graphics libraries. This wasn't always the case, but I encountered this while trying to run a Java text editor on 10.10 the other day. Run the following command to install a JDK that has these libraries:
sudo apt-get install openjdk-6-jdk
EDIT: Actually, looking at my config, you might need the JRE. If that's the case, run:
sudo apt-get install openjdk-6-jre
回答2:
I stopped getting this exception when I installed default-jdk using apt. I'm running Ubuntu 14.04 (Trusty Tahr), and the problem appears to have been the result of having a "headless" Java installed. All I did was:
sudo apt-get install default-jdk
回答3:
In my case
-Djava.awt.headless=true
was set (indirectly by a Maven configuration). I had to actively use
-Djava.awt.headless=false
to override this.
回答4:
I too had OpenJDK on my Ubuntu machine:
$ 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:
sudo apt-get autoremove openjdk-7-jre-headless
How to install the JDK on Ubuntu (Linux)
回答5:
Check what your environment variable DISPLAY's value is. Try running a simple X application from the command line. If it works, check DISPLAY's value for the right value.
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?
回答6:
I would check with another Java implementation/vendor. Preferrably Oracle/Sun Java: http://www.java.com/en/ . The open-source implementations unfortunately differ in weird ways.
回答7:
Check your X Window environment variables using the "env" command.
回答8:
Use JFrame instead of Frame. And do not extend from 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. Extending here is wrong the use of OOP.
回答9:
This command worked for me.
Sudo dnf install java-1.8.0-openjdk
(Fedora)
Sudo apt-get install java-1.8.0-openjdk
Should work for Ubuntu.
来源:https://stackoverflow.com/questions/5362512/unable-to-run-java-gui-programs-with-ubuntu