1

I tried to run a java awt program but it gives this error:

I am using lubuntu 18.04 and openjdk 11

Exception in thread "main" java.lang.UnsatisfiedLinkError: Can't load library: /usr/lib/jvm/java-11-openjdk-amd64/lib/libawt_xawt.so
    at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2630)
    at java.base/java.lang.Runtime.load0(Runtime.java:768)
    at java.base/java.lang.System.load(System.java:1837)
    at java.base/java.lang.ClassLoader$NativeLibrary.load0(Native Method)
    at java.base/java.lang.ClassLoader$NativeLibrary.load(ClassLoader.java:2442)
    at java.base/java.lang.ClassLoader$NativeLibrary.loadLibrary(ClassLoader.java:2498)
    at java.base/java.lang.ClassLoader.loadLibrary0(ClassLoader.java:2694)
    at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2648)
    at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:830)
    at java.base/java.lang.System.loadLibrary(System.java:1873)
    at java.desktop/java.awt.Toolkit$3.run(Toolkit.java:1399)
    at java.desktop/java.awt.Toolkit$3.run(Toolkit.java:1397)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.desktop/java.awt.Toolkit.loadLibraries(Toolkit.java:1396)
    at java.desktop/java.awt.Toolkit.<clinit>(Toolkit.java:1429)
    at java.desktop/java.awt.Component.<clinit>(Component.java:621)

this is the code I have written in my editor:

class calculator extends Frame{  
    calculator(){  
    Button b=new Button("click me");  
    b.setBounds(30,100,80,30);  
    add(b);  
    setSize(300,300); 
    setLayout(null);  
    setVisible(true); 
    }  
    
    public static void main(String args[]){  
    calculator f=new calculator();  
    }}  

Can I use java awt in openjdk 11 or do I have to separately install and integrate it?

I am not using headless!

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • I am not using headless still i am getting this error – programmer master May 27 '21 at 10:24
  • 1
    If it can't load `libawt_xawt.so` that's a strong indication that some X11 related libraries are not installed which should only be possible on a "server"-style installation: is this running in a Docker container or some other server that doesn't have the X11 UI libraries installed? – Joachim Sauer May 27 '21 at 10:30
  • What JDK package(s) have you installed? [This SO question](https://stackoverflow.com/questions/67391845/exception-java-lang-unsatisfiedlinkerror-when-trying-to-open-allure-reports-in-w) indicates that the `libawt_xawt.so` itself could be missing which would indicate an incomplete JDK installation (such as only installing the headless part of the JDK). – Joachim Sauer May 27 '21 at 10:35
  • It is not running in a docker container. I tried to reinstall openjdk on my machine but it did not work perhaps it may be my system`s problem or is it? – programmer master May 27 '21 at 10:38
  • Also, include `java -version` it could be you need to 'update alternatives' to point at the correct jdk. Right now it might be pointing at the headless jdk if you have both installed. – matt May 27 '21 at 10:43
  • When I start up my system It shows "System program problem detected" is that problem related to this? how to I come to know about that? – programmer master May 27 '21 at 10:45
  • 2
    @programmermaster: you've still not answered the question about the **exact name** of the openjdk packages you've installed. My strong suspicion is that contrary to your claim you've only installed the `-headless` version of the JDK/JRE. You should have either `openjdk-11-jre` or `openjdk-11-jdk` installed (I suggest the later). – Joachim Sauer May 27 '21 at 10:46
  • my application is targeting windows 2000 and before so I want to learn java awt – programmer master May 27 '21 at 10:48
  • I have installed openjdk-11-jdk – programmer master May 27 '21 at 10:50
  • 1
    Windows 2000? Are you sure? The OS that got all development on it discontinued in 2010? The OS that only supported Java 6, since Java 7 dropped all support for it? I'm pretty sure you'd be hard-pressed to find any OpenJDK builds that work on it, let alone are supported on that platform. And besides this: any platform that *does* support AWT also supports Swing, as that's just built ontop of AWT. – Joachim Sauer May 27 '21 at 10:51
  • java works properly on my linux xubuntu counterpart just fine – programmer master May 27 '21 at 10:51
  • Try `ldd /usr/lib/jvm/java-11-openjdk-amd64/lib/libawt_xawt.so` on your shell to figure out what's missing. – Joachim Sauer May 27 '21 at 10:52
  • thanks for the information I will surely try swing and Inform you if it works – programmer master May 27 '21 at 10:54
  • It is showing that the file is corrupt even though I installed openjdk corretly – programmer master May 27 '21 at 10:56
  • swings working just fine on my machine thanks for informing – programmer master May 27 '21 at 10:57

2 Answers2

7

You can fix run with JVM argument passing:

-Djava.awt.headless=true
BeCase
  • 141
  • 2
  • 5
0

I fixed it I installed openjdk 8 on my computer (not headless!) and reastarting my system