javaswinguser-interfaceubunturuntime-error

Unable to run Java GUI programs with Ubuntu


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.


Solution

  • 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