Java Can't connect to X11 window server using 'localhost:10.0' as the value of the DISPLAY variable
Asked Answered
K

34

126

I have a script using java to connect to display X11 in the port 10.0 at localhost

but i get always this error

java.lang.InternalError: 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:62)
    at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:178)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:142)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:186)
    at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:82)
    at sun.awt.X11.XToolkit.<clinit>(XToolkit.java:112)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:186)
    at java.awt.Toolkit$2.run(Toolkit.java:849)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:841)
    at ij.io.Opener.openJpegOrGif(Opener.java:367)
    at ij.io.Opener.openImage(Opener.java:220)
    at ij.io.Opener.openImage(Opener.java:249)
    at ij.io.Opener.open(Opener.java:116)
    at ij.IJ.open(IJ.java:1112)
    at ij.macro.Functions.open(Functions.java:2006)
    at ij.macro.Functions.doFunction(Functions.java:129)
    at ij.macro.Interpreter.doStatement(Interpreter.java:205)
    at ij.macro.Interpreter.doBlock(Interpreter.java:515)
    at ij.macro.Interpreter.runUserFunction(Interpreter.java:278)
    at ij.macro.Interpreter.getFactor(Interpreter.java:1200)
    at ij.macro.Interpreter.getTerm(Interpreter.java:1162)
    at ij.macro.Interpreter.getExpression(Interpreter.java:1145)
    at ij.macro.Interpreter.getBooleanExpression(Interpreter.java:881)
    at ij.macro.Interpreter.getLogicalExpression(Interpreter.java:857)
    at ij.macro.Interpreter.getBoolean(Interpreter.java:850)
    at ij.macro.Interpreter.doIf(Interpreter.java:829)
    at ij.macro.Interpreter.doStatement(Interpreter.java:217)
    at ij.macro.Interpreter.doBlock(Interpreter.java:515)
    at ij.macro.Interpreter.doStatement(Interpreter.java:241)
    at ij.macro.Interpreter.doIf(Interpreter.java:831)
    at ij.macro.Interpreter.doStatement(Interpreter.java:217)
    at ij.macro.Interpreter.doStatements(Interpreter.java:195)
    at ij.macro.Interpreter.run(Interpreter.java:99)
    at ij.macro.Interpreter.run(Interpreter.java:65)
    at ij.macro.Interpreter.run(Interpreter.java:75)
    at ij.plugin.Macro_Runner.runMacro(Macro_Runner.java:127)
    at ij.plugin.Macro_Runner.runMacroFile(Macro_Runner.java:112)
    at ij.IJ.runMacroFile(IJ.java:103)
    at ij.ImageJ.main(ImageJ.java:517)

I have tried everything to solve this problem like :

export DISPLAY=:10.0
export DISPLAY=localhost:10.0

I tried also the port 0.0 but i get always the same error

after trying xhost

xhost +local:all
xhost:  unable to open display ""
xhost:  unable to open display ":10.0"

how can i fix this i thought that the X Server is not runing so i tried startx its says its runing at that port

my system is Ubuntu server edition 10.04

Keli answered 15/4, 2012 at 20:26 Comment(1)
If also using a VNC viewer via ssh: after modifying the xhost access list, it is necessary to reopen the SSH connection. At least on my current setup (VNC to Debian), I infer that it needs to get an updated xhost access list.Manella
C
50

This command helped me to solve the problem:

export DISPLAY=:0
Catcher answered 25/2, 2013 at 13:54 Comment(3)
unset DISPLAY helped me (with export DISPLAY=:0 I got the error Can't connect to X11 window server using ':0'Scully
well sometime - it won't work on linux then its good to install Xvfb with apt and run it on any screen like: Xvfb :1 and then set variable in build phase -- export DISPLAY=:1 --> this perfectly works.Nephrolith
Please, read: this #20608277Landis
C
75

You need to specify the -Djava.awt.headless=true parameter at startup time.

Coblenz answered 15/4, 2012 at 20:42 Comment(10)
Thanks alot but where i have to specify this exactly ?Keli
Exception in thread "main" java.awt.He adlessException at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:1 73)Keli
Are you running a security manager? If so, you maybe have to grant permission to.Coblenz
thanks the problem was that the X Server is running in another portKeli
i got the same problem on another server and its because the driverKeli
This solved my problem with a Jenkins build. It just suddenly STOPPED working, and adding this flag to the maven command line fixed it. Thanks!Julieannjulien
This worked for me. In Jenkins -> Build -> Goals and options -> -Djava.awt.headless=true test In my case clean compile -Djava.awt.headless=true testNkrumah
The asker's program wants to do ij.io.Opener.openJpegOrGif, we can guess that probably requires a GUI. So headless would result in another error: java.awt.HeadlessException.Graphology
with this true - i got error: java.awt.AWTException: headless environmentNephrolith
This prevents the error, but now the GUI popup that is supposed to go to my main display just does not exist at all...Folkways
D
53

Remove the DISPLAY variable

unset DISPLAY

This helps in most cases (e.g. starting application servers or other java based tools) and avoids to modify all that many command lines.

It can also be comfortable to add it to the .bash_profile for a dedicated app-server/tools user.

Doukhobor answered 10/12, 2012 at 21:20 Comment(4)
it didn't help me at all. I got this: AWT Can't connect to X11 window server using...Nephrolith
If your app uses awt then you must set DISPLAY correctly. This is for apps not using awt.Doukhobor
When playing around with this, make sure you restart mobaXterm after adding the unset command in your bashfile. Since the Display variable will be set from before and just running your bash file won't change that.Prue
This works when I use Putty + Xming Server:0.0. Unfortunately, I have the same error when using wsl2. I haven`t tried mobaXterm, but, you need to restart Xming after adding the unset command as wellRing
C
50

This command helped me to solve the problem:

export DISPLAY=:0
Catcher answered 25/2, 2013 at 13:54 Comment(3)
unset DISPLAY helped me (with export DISPLAY=:0 I got the error Can't connect to X11 window server using ':0'Scully
well sometime - it won't work on linux then its good to install Xvfb with apt and run it on any screen like: Xvfb :1 and then set variable in build phase -- export DISPLAY=:1 --> this perfectly works.Nephrolith
Please, read: this #20608277Landis
M
30

I think you are working in sudo mode.Please checkout to the user mode and try again

Mcentire answered 5/7, 2017 at 9:7 Comment(5)
This was my problem as well. Seems odd to not be able to run as root though, right?Dulciana
Why I can't use sudo ?Buddle
@javajavajava yupMcentire
Worked for me.. think the X11 server can only be started by the owner of the current session in my case rootBriannebriano
Seems the same problem for me. Had to start Xvfb from out my gitlab pipeline, could not do that in the docker image itself.Inaccurate
A
10

In case anybody trying to run the automated unit tests via maven-surefire-plugin on CI(jenkins,..), and getting the above mentioned error, be sure to update your surefire plugin configuration :

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>${maven-surefire-plugin.version}</version>
     <configuration>
            <systemPropertyVariables>
                <java.awt.headless>true</java.awt.headless>
            </systemPropertyVariables>
      </configuration>
</plugin>
Acrimony answered 22/5, 2014 at 13:47 Comment(1)
how to do it in gradle and also how to set the property value in it.Ifni
A
8

This will fix it:

/usr/bin/java -Djava.awt.headless=true $Your_program
Alpha answered 17/7, 2012 at 15:0 Comment(2)
Did not fix mineItalicize
This worked for me. Thank you. I was getting the error because I'm using poi.Nannie
P
7

For me logging in as -Y instead of -X worked.

In case you've got untrusted X11 as shown below, then try -Y flag instead (if you trust the host):

Warning: untrusted X11 forwarding setup failed: xauth key data not generated

Prior answered 3/7, 2017 at 10:46 Comment(0)
C
6

If you are trying to export display using su and it still doesn't work. This is what worked for me. Try X11 forwarding for sudo users.

Connect the remote host using the -X option with ssh.

# ssh -X root@remote-host

Now list the coockie set for the current user.

# xauth list $DISPLAY
    node01.thegeekdiary.com/unix:10  MIT-MAGIC-COOKIE-1  dacbc5765ec54a1d7115a172147866aa
# echo $DSIPLAY
    localhost:10.0

Switch to another user account using sudo. Add the cookie from the command output above to the sudo user.

# sudo su - [user]
# xauth add node01.thegeekdiary.com/unix:10  MIT-MAGIC-COOKIE-1  dacbc5765ec54a1d7115a172147866aa

Export the display from step 2 again for the sudo user. Try the command xclock to verify if the x client applications are working as expected.

# export DISPLAY=localhost:10.0

source: https://www.thegeekdiary.com/how-to-set-x11-forwarding-export-remote-display-for-users-who-switch-accounts-using-sudo/

Cooky answered 14/4, 2020 at 11:57 Comment(0)
P
6

This fixed my problem

xhost +

but Be aware that xhost + completely deactivates authentication and allows everyone to access all application on your screen.

xhost +si:localuser:root seems to work similar with proper authentication.

Paramorph answered 25/6, 2020 at 13:41 Comment(0)
I
4

After several days of futile effort of installing glassfish on raspberry pi 2 with headless fedora 22, Below worked for me without a hitch

 unset DISPLAY
java -Djava.awt.headless=true -jar glassfissh-installer-v2ur2-b04-linux.jar

got my help from here

Interplead answered 27/2, 2016 at 1:26 Comment(0)
F
4

First: start XQuartz

Second: ssh -X user@ip_address

...: start your process

if you ssh and then start XQuartz you will get that error

Footrest answered 28/10, 2016 at 21:5 Comment(0)
N
3

First do this either in Build Phase of Jenkins if using or set in /etc/profile:

unset DISPLAY
export DISPLAY=:0

then set this property either in java code or using maven: -Djava.awt.headless=false

Nephrolith answered 5/9, 2018 at 7:42 Comment(0)
S
2

I was using Xming and got similar error. Following steps were taken to fix the issue:

  1. In Xming launch check the box no access control.
  2. In putty ran the following command: DISPLAY=XXX.XXX.XXX.XX:0.0; export DISPLAY

Replace XXX.XXX.XXX.XX with your IP address.

Solita answered 13/4, 2017 at 14:38 Comment(1)
I am in the same configuration, however it didn't work. I needed to add the IP executing X11 in X0.hosts fileIneducation
G
2

Solved. I just logout and login with xorg!

Gasteropod answered 5/12, 2018 at 5:47 Comment(1)
export DISPLAY=:0 or export DISPLAY=:1 did not work for me. Logging out and logging in solved it for me.Disoperation
D
2

check whether $DISPLAY variable is set or not, with the below command:

echo $DISPLAY

if the display variable is not set, run the below command to set, (even if it is set, you can have below one for your session)

export DISPLAY=:0.0

in also have the x display location as :0.0

Disendow answered 23/4, 2020 at 17:1 Comment(0)
J
1

Michael-O gave useful approach to solve the problem. Another way to solve this is by starting the server with Putty Console.

Justiciar answered 14/12, 2015 at 8:11 Comment(1)
can you maybe explain a Little more how that would work? (I have never used putty)Prue
M
1

In my case there was no space left in my machine and I faced the same issue. Some times it could be the space issue. Check the space in your Linux/Unix environment and make sure your machine have enough space.

Monde answered 2/1, 2017 at 5:36 Comment(0)
K
1

For Ubuntu 17.10 Install X virtual frame buffer (xvfb)

apt install xvfb

And added these lines to the /etc/profile file...

# Start the X virtual frame buffer (Xvfb)
if [ -f /usr/X11R6/bin/Xvfb ]; then
/usr/X11R6/bin/Xvfb :1 -screen 0 1366x768x32
fi

# Set the DISPLAY variable for the X virtual frame buffer (Xvfb)
export DISPLAY=localhost:1.0
Kincardine answered 16/2, 2018 at 19:15 Comment(1)
Now I just get "Exception in thread "main" java.awt.AWTError: Can't connect to X11 window server using 'localhost:1.0' as the value of the DISPLAY variable." on Ubuntu 16.04. The Xvfb binary now seems to be located at /usr/bin, not /usr/X11R6/bin, but gives me "Couldn't add screen 0 (EE)".Troublous
U
1

I have fixed this issue by logging in using Xorg. By default, I have used Wayland. It looks like Wayland eliminates most of the design flaws of the Xorg it has its own issues.enter image description here

Unbeatable answered 25/2, 2020 at 9:38 Comment(0)
M
1

I had the same issue on the Linux server I was working on. Connecting java to a X11 display worked on the head node, but not on any other. After contacting the administrator, it turned out that the current version of our job-scheduling system (SLURM) did not support X11 forwarding. They had to update SLURM (newer versions of SLURM support it) for it to work.

Mook answered 1/11, 2020 at 12:9 Comment(0)
L
0

the only way i got it to work was running the script with a template. e.g. sudo ./glassfish-3.1.2.2-unix.sh -s template

This installs Glassfish in Silent Mode. http://docs.oracle.com/cd/E18930_01/html/821-2427/ghmva.html

Linnette answered 24/4, 2013 at 17:2 Comment(0)
P
0

I run into the same error with you when i run the jconsole command at remote. I want to modify a parameter at jconsole that run on a remote Linux host, i can login the host use the secureCRT, the terminal throw this error information. Fortunately, when use the Putty, it's ok. Weird....

Paraclete answered 2/5, 2013 at 5:48 Comment(0)
S
0

If you see this error in Hudson, try to remove the .java directory from your home directory, it may work for you.

Slipslop answered 1/11, 2013 at 16:15 Comment(0)
N
0

If you're triggering your code from Jenkins, enabling the option "Start Xvfb before the build, and shut it down after" might help. It helped me.

Nepos answered 13/9, 2017 at 5:46 Comment(0)
L
0

change to a another user and try except root. it works for me.

Lilybelle answered 20/9, 2017 at 18:7 Comment(0)
F
0

I just didn't log out of root before running ./studio.sh All set.

Fogy answered 6/1, 2018 at 21:41 Comment(0)
S
0

In my case this error was not related to the DISPLAY port. I was trying to load an XML into Windchill (a PLM-software) and received only the above error on the terminal. In a logfile I found the report that my XML-file was corrupt. Maybe someone has a similar problem and can use this answer.

Saundrasaunter answered 4/9, 2018 at 7:56 Comment(0)
T
0

Mine issue was with the firewall. Disabled it temporarily.

[EDIT] And, the server hostname was pointing to another IP. Set it to simply localserver. strace xclock helped to debug this issue.

Tetrastichous answered 28/10, 2019 at 5:18 Comment(0)
P
0

For me none of the above worked, but after long search this worked for me.

export DISPLAY=localhost:20.0
Pitre answered 14/9, 2020 at 13:44 Comment(0)
H
0
  1. export localhost:1

  2. -Djava.awt.headless=true

Humorist answered 12/4, 2022 at 12:45 Comment(0)
E
0

In my case I have this exact error when I was launching the Jetbrains IDEs, such as Pycharm, IntelliJ IDEA...

I typed this in the same terminal that I was using to start the IDE to solve the problem:

export DISPLAY=:1

Then I started the IDE from the same terminal issuing the command pycharm and it worked like a charm.

The output of both IDEs was: enter image description here

Elea answered 22/2, 2023 at 14:11 Comment(0)
F
0

run below cmd fixed my issue. works for my case

unset DISPLAY

Faroff answered 12/7, 2023 at 9:18 Comment(0)
T
-1

If you start application on a remote server while logged in by ssh then another way would be to start ssh with -x parameter or add ForwardX11 no in your /etc/ssh/ssh_config. In this case ssh will not create environment variable DISPLAY.

Tannate answered 12/10, 2015 at 11:16 Comment(0)
C
-1

For me, the problem was that xorg-x11-xauth wasn't installed. I installed it and then it worked.

The packages that I have now are:

  • libX11-common-1.6.3-2.el6.noarch
  • libX11-1.6.3-2.el6.i686
  • libX11-1.6.3-2.el6.x86_64
  • xorg-x11-drv-ati-firware-7.6.1-2.el6.noarch
  • xorg-x11-xauth-1.0.9-1.el6.x86_64
Cadency answered 1/11, 2016 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.