Java无法使用“localhost:10.0”作为DISPLAY变量的值连接到X11窗口服务器

123
我有一个使用Java连接本地端口10.0的X11显示屏的脚本,但是我一直遇到这个错误。
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)

我已经尝试了各种方法来解决这个问题,例如:

export DISPLAY=:10.0
export DISPLAY=localhost:10.0

我尝试使用端口0.0,但是仍然遇到了相同的错误。在尝试xhost之后。
xhost +local:all
xhost:  unable to open display ""
xhost:  unable to open display ":10.0"

我该如何解决这个问题?

我认为X Server没有运行,因此我尝试了startx,但它显示该端口正在运行。

我的系统是Ubuntu服务器版10.04。


如果您还通过ssh使用VNC查看器:在修改了xhost访问列表之后,有必要重新打开SSH连接。至少在我的当前设置(从VNC到Debian),我推断需要获取更新的xhost访问列表。 - Technophile
34个回答

3

在尝试在树莓派 2 上安装 headless fedora 22 中 Glassfish 几天努力失败后,以下方法顺利地为我解决了问题。

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

got my help from here


3

如果使用Jenkins,则首先在Build Phase中执行此操作,否则在/etc/profile中设置:

unset DISPLAY
export DISPLAY=:0

然后在Java代码中或使用Maven设置此属性: -Djava.awt.headless=false


2
解决了。我只是注销并使用xorg重新登录!

1
export DISPLAY=:0或export DISPLAY=:1对我没有用。注销并重新登录解决了我的问题。 - Optimus

2

我使用Xming时遇到了类似的错误。以下是解决问题的步骤:

  1. 在Xming中启动,勾选“无访问控制”框。
  2. 在putty中运行以下命令:DISPLAY=XXX.XXX.XXX.XX:0.0; export DISPLAY

XXX.XXX.XXX.XX替换为您的IP地址。


我使用了相同的配置,但是它没有起作用。我需要在X0.hosts文件中添加执行X11的IP地址。 - Christophe Moine

2
检查是否设置了$DISPLAY变量,使用以下命令:
echo $DISPLAY

如果显示变量未设置,请运行以下命令进行设置(即使已经设置,您也可以在本次会话中使用以下命令)。
export DISPLAY=:0.0

中,x 显示位置也为 :0.0

1

针对Ubuntu 17.10 安装X虚拟帧缓冲器(xvfb)

apt install xvfb

"将以下行添加到/etc/profile文件中..."
# 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

3
现在我在Ubuntu 16.04上运行程序时,遇到了异常信息:"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."。现在Xvfb二进制文件似乎位于/usr/bin而不是/usr/X11R6/bin,但仍然出现错误提示:"Couldn't add screen 0 (EE)"。 - Chris Jenks

1

Michael-O提供了有用的解决问题的方法。另一种解决方法是通过Putty控制台启动服务器。


你能否再详细解释一下它是如何工作的?(我从未使用过putty) - mrk

1
在我的情况下,我的计算机没有剩余空间,我遇到了相同的问题。有时候这可能是空间问题。检查你的Linux/Unix环境中的空间,确保你的计算机有足够的空间。

1
我在工作中使用的Linux服务器上也遇到了同样的问题。将Java连接到X11显示器可以在主节点上运行,但在其他节点上却无法运行。联系管理员后,发现我们当前的作业调度系统(SLURM)不支持X11转发。他们必须更新SLURM(新版本的SLURM支持它)才能使其正常工作。

0

我已经通过使用Xorg登录来解决了这个问题。默认情况下,我使用的是Wayland。看起来Wayland消除了Xorg的大部分设计缺陷,但它也有自己的问题。在此输入图像描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接