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.
This question is related to
java
swing
user-interface
ubuntu
runtime-error
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
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
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.
Check your X Window environment variables using the "env" command.
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.
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.
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?
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.
Source: Stackoverflow.com