-1

I have a problem working connecting to X11 and DISPLAY variable

I'm working with a Java file in my Raspberry Pi Zero that is using JFrame and I want the window created by JFrame to show up in a monitor that is connected to my Raspberry, but I keep getting this exception. Any ideas? I'm working with Java OpenJDK version "1.8.0_312"

Exception in thread "main" java.awt.AWTError: Can't connect to X11 window server using 'localhost:10.0' as the value of the DISPLAY variable.
    at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
    at sun.awt.X11GraphicsEnvironment.access$200(X11GraphicsEnvironment.java:65)
    at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:115)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:74)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:103)
    at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:82)
    at java.awt.Window.initGC(Window.java:475)
    at java.awt.Window.init(Window.java:495)
    at java.awt.Window.<init>(Window.java:537)
    at java.awt.Frame.<init>(Frame.java:420)
    at javax.swing.JFrame.<init>(JFrame.java:233)
    at Prueba.main(Prueba.java:5)

I tried to start xterm or xhost to check everything was working fine but I have this problem:

No protocol specified
xterm: Xt error: Can't open display: :0

I am using Putty as my SSH client.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
jlan
  • 19
  • 5
  • Looks like you haven't installed zthe x11 components of you OS – Jens Apr 05 '23 at 16:19
  • I have installed X11 and it works if I type startx – jlan Apr 05 '23 at 16:25
  • Does this answer your question? [Java Can't connect to X11 window server using 'localhost:10.0' as the value of the DISPLAY variable](https://stackoverflow.com/questions/10165761/java-cant-connect-to-x11-window-server-using-localhost10-0-as-the-value-of-t) – hc_dev Apr 05 '23 at 17:51
  • Won't work, but thanks anyways – jlan Apr 06 '23 at 08:46

2 Answers2

1

I just got it, it was as simple as writing sudo java -jar instead of java -jar, not sure why but it works now

jlan
  • 19
  • 5
  • 1
    Nice! You may want to see if you can run X as your normal user if you're worried about giving your Java code root access. Also, sudo may have worked because it will change your environment variables such as DISPLAY. If `sudo -E java -jar ...` doesn't work it's most likely related to environment variables, the -E option preserves the environment from outside the sudo command – M Virts Apr 10 '23 at 16:27
0

Based on the localhost:10.0 value of DISPLAY, it seems likely that you are connecting to the raspberry pi via SSH and your SSH client is attempting to do X forwarding (allowing remote programs to open windows on your local computer). Disable X forwarding in your SSH client or set your DISPLAY variable to something else to avoid this. I would try export DISPLAY=:0 as a starting point for testing.

If that doesn't help, try running a known X application like xterm to see if it will connect to the X server and create a window. If this fails, you may need to fix your X server configuration, or an X server may not be running.

If that succeeds, check the DISPLAY variable that works for the other program (echo $DISPLAY) and try using whatever value is set there. (e.g. export DISPLAY=:0)

Note that X display numbers usually correspond to a TCP port number where the X server is listening. The display number is 6000 less than the port number, so display 0 listens on port 6000, display 10 listens on 6010. This means you can use a command like netstat -tlp to list out programs that are listening on specific ports and check if anything is listening on port 6010, corresponding to localhost:10.0 in your DISPLAY variable. If the process listening on 6010 is ssh, your client is attempting to provide X forwarding.

Also note that on most systems X servers have an authentication mechanism that prevents users that didn't start a session from connecting. (look up XAuthority file for more info) Usually the root user can open windows on any X server, so it may be worth trying as the root user to test.

M Virts
  • 173
  • 5
  • Thanks! "export DISPLAY=:0" won't work as I keep getting the same exception. When I try to start xterm or xhost I have the same problem: "No protocol specified xterm: Xt error: Can't open display: :0". Just checked that ssh is not listening on port 6010. Any ideas?? – jlan Apr 06 '23 at 07:30