Unable to run Java GUI programs with Ubuntu
Asked Answered
L

9

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.

Litigious answered 19/3, 2011 at 13:54 Comment(0)
N
52

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
Nubbin answered 19/3, 2011 at 14:4 Comment(3)
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.Litigious
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.Litigious
In my case I needed jdk indeed, not jre. I installed openjdk-11, though.Burushaski
A
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
Alphosis answered 23/11, 2014 at 23:48 Comment(1)
that installs openjdk-7-jre for me, which fixes it sweetBypass
O
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.

Orthodoxy answered 12/4, 2016 at 15:34 Comment(0)
C
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)

Chelseychelsie answered 6/10, 2014 at 16:33 Comment(0)
P
0

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

Phyllode answered 19/3, 2011 at 14:0 Comment(5)
No such variable exists. as i tried env|grep XW but nothing was thereLitigious
I didn't mean a variable called exactly XWindow.Phyllode
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/databaseLitigious
Try: env | grep -i display <br/> It gave me: DISPLAY=:0.0 This should be the essential X-Window env var for non-headless-ness.Phyllode
env|grep -i display gives: DISPLAY=:0.0Litigious
A
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.

Augite answered 19/3, 2011 at 14:0 Comment(4)
But can you say what the problem with this. why it is showing me error message. because it is running just fine in windowsLitigious
Maybe just try to use an other JRE than IcedTea6Augite
JFrame is built on top of Frame, so changing that wouldn't matter much. Also, whether or not extending (J)Frame wouldn't help anything in case of a HeadlessException.Theorbo
Anyway it is better style not extending.Augite
H
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?

Hyperplasia answered 19/3, 2011 at 14:15 Comment(6)
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 problemLitigious
Also try: export DISPLAY=<:0.0 or some other stuff> and then run your app(s).Phyllode
AFAIK, evince is not Java-based, so that would not matter much in this discussion.Phyllode
when i tried the command export DISPLAY=<:0.0 it says: bash: :0.0: No such file or directoryLitigious
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.Hyperplasia
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.Hyperplasia
P
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.

Phyllode answered 19/3, 2011 at 14:20 Comment(2)
I am using the java implementation downloaded from oracle's siteLitigious
But is it the "regular" Java or OpenJDK? Try the thing not called OpenJDK.Phyllode
B
-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.

Brenna answered 5/9, 2017 at 10:5 Comment(2)
dnf is for Fedora, not Ubuntu, so that's probably no proper answer.Reginareginald
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.Brenna

© 2022 - 2024 — McMap. All rights reserved.