36

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
codeomnitrix
  • 4,179
  • 19
  • 66
  • 102

9 Answers9

50

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
Yuri
  • 4,254
  • 1
  • 29
  • 46
Kaleb Brasee
  • 51,193
  • 8
  • 108
  • 113
  • Hey but this is working just fine on my friends pc as he is using ubuntu 10.10 with the same jdk i have installed. – codeomnitrix Mar 19 '11 at 14:07
  • 1
    Ok Kaleb this is now working fine but still one problem that previously whenever i type `java Fram` and hit tab then it completes the name of the class file but right now it is not doing so? And one more thing there is only one file in that folder. – codeomnitrix Mar 19 '11 at 14:31
  • In my case I needed `jdk` indeed, not `jre`. I installed openjdk-11, though. – Eyjafl Aug 28 '22 at 14:46
10

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
wachr
  • 1,027
  • 12
  • 12
4

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
2

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)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
prayagupa
  • 30,204
  • 14
  • 155
  • 192
0

Check your X Window environment variables using the "env" command.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
KarolDepka
  • 8,318
  • 10
  • 45
  • 58
  • No such variable exists. as i tried env|grep XW but nothing was there – codeomnitrix Mar 19 '11 at 14:04
  • I didn't mean a variable called exactly XWindow. – KarolDepka Mar 19 '11 at 14:10
  • Hey here is what the command: env|grep X shows: `XDG_SESSION_COOKIE=e09e4f1f12d93b7c37a0cab100000007-1300541756.556846-1851639608 XDG_CONFIG_DIRS=/etc/xdg/xdg-xubuntu:/etc/xdg XDG_DATA_DIRS=/etc/xdg/xdg-xubuntu:/usr/share/xubuntu:/usr/local/share/:/usr/share/:/usr/share XAUTHORITY=/var/run/gdm/auth-for-vinit-90yAl9/database` – codeomnitrix Mar 19 '11 at 14:13
  • Try: env | grep -i display
    It gave me: DISPLAY=:0.0 This should be the essential X-Window env var for non-headless-ness.
    – KarolDepka Mar 19 '11 at 14:14
  • env|grep -i display gives: `DISPLAY=:0.0` – codeomnitrix Mar 19 '11 at 14:17
0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jakob Alexander Eichler
  • 2,988
  • 3
  • 33
  • 49
0

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?

Allen
  • 2,228
  • 18
  • 22
  • 1
    Hey allen i tried to run `evince` from command line and it works just fine. but when i tried to run `DISPLAY=:0.0 java FrameDemo` then the same problem – codeomnitrix Mar 19 '11 at 14:21
  • Also try: export DISPLAY=<:0.0 or some other stuff> and then run your app(s). – KarolDepka Mar 19 '11 at 14:22
  • AFAIK, evince is not Java-based, so that would not matter much in this discussion. – KarolDepka Mar 19 '11 at 14:24
  • when i tried the command `export DISPLAY=<:0.0` it says: `bash: :0.0: No such file or directory` – codeomnitrix Mar 19 '11 at 14:27
  • DISPLAY must contain something like 'hostname:x.y' or ':x.y'. The '<' and the '>' were to indicate what was to be substituted. Also, @karolrvn, the exception here is caused by JVM not finding one or more of display, mouse, or keyboard when it needs them. – Allen Mar 19 '11 at 19:35
  • Make the JVM dump its environment variables so you can see what is happening. Get a map of these settings from java.lang.System.getProperties() and print them out. – Allen Mar 19 '11 at 19:47
0

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.

KarolDepka
  • 8,318
  • 10
  • 45
  • 58
-2

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.

  • `dnf` is for Fedora, not Ubuntu, so that's probably no proper answer. – Dunedan Sep 05 '17 at 10:27
  • It really depends on your basic knowledge of Linux distributions. I have been able to use Ubuntu commands to fix Fedora problems in the past, by just knowing the equivalent commands.Plus this answer just increases the scope of the issue raised since I faced the same thing with Fedora. – Tanisha Shrotriya Sep 07 '17 at 08:54