简体   繁体   English

java.awt.AWTError:无法使用“:0”作为DISPLAY变量的值连接到X11窗口服务器

[英]java.awt.AWTError: Can't connect to X11 window server using ':0' as the value of the DISPLAY variable

I have been trying to run a Java AWT based application on an Ubuntu VM inside Docker . 我一直试图在Docker内部的Ubuntu VM上运行基于Java AWT的应用程序 The application is a very simple one and it flawlessly runs on Windows through Eclipse. 该应用程序非常简单,可以通过Eclipse在Windows上完美运行。 It simply opens up a window and prints hello world!. 它只是打开一个窗口并打印您好世界!。 In fact when I export the jar file in to an Ubuntu VM that is running on my Windows host also gives me the same output as Windows when I run the jar file through “java -jar JFrameDocker.jar” . 实际上,当我将jar文件导出到在Windows主机上运行的Ubuntu VM时,通过“ java -jar JFrameDocker.jar”运行jar文件时,也会提供与Windows相同的输出。

However the story is not same when I try to run it inside docker. 但是,当我尝试在docker中运行时,故事并不相同。 The instructions that I have specified in the Dockerfile executes without any error but when I run the application it throws “java.awt.AWTError: Can't connect to X11 window server using ':0' as the value of the DISPLAY variable” . 我在Dockerfile中指定的指令执行没有任何错误,但是当我运行应用程序时,它抛出“ java.awt.AWTError:无法使用':0'作为DISPLAY变量的值连接到X11窗口服务器” Trust me guys, I have tried many a times to resolve it but no luck till now. 相信我,我已经尝试过很多次了,但是到目前为止还没有运气。 Like setting the DISPLAY variable value multiple times, starting X server using xterm and xeyes, going through every possible article that I found on the Web. 就像多次设置DISPLAY变量值一样,使用xterm和xeyes启动X服务器,仔细阅读我在Web上找到的所有文章。 But nothing seems to be working here. 但似乎没有任何工作在这里。 I am providing all my files here that could get you a better understanding of my problem. 我在这里提供所有文件,可以使您更好地了解我的问题。 By the way this doesn't answer my question as same error is being thrown even after making the changes. 顺便说一句, 不能回答我的问题,因为即使进行更改后也会抛出相同的错误。

This is my java file. 这是我的java文件。

package com.etp;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class JFrameDockerTest {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        JLabel lable = new JLabel("Hello World");
        panel.setLayout( new FlowLayout() );  
        frame.add(panel);
        panel.add(lable);
        frame.setVisible(true);
        frame.setSize(800, 600);
    }

}

This is my Dockerfile: 这是我的Dockerfile:

FROM java:8
ENV DISPLAY :0
ADD JFrameDocker.jar JFrameDocker.jar
CMD ["java","-jar", "JFrameDocker.jar"]

I have used below docker commands to build image and run it. 我已经使用了以下docker命令来构建映像并运行它。

Docker Build : sudo docker build -t jframedocker . Docker构建: sudo docker build -t jframedocker .

Docker Run : sudo docker run jframedocker Docker Run: sudo docker run jframedocker

Output Without Error Screenshot: (Without Docker) 输出无错误截图:(无Docker)

Output with Docker: Docker的输出:

etp@etp-VirtualBox:~/Downloads/JFrameDocker$ sudo docker run jframedocker
Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.awt.AWTError: Can't connect to X11 window server using ':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 java.awt.Frame.<init>(Frame.java:385)
    at javax.swing.JFrame.<init>(JFrame.java:189)
    at com.etp.JFrameDockerTest.main(JFrameDockerTest.java:12)
    ... 5 more

You are using java:8 base image which most likely doesn't provide a graphical environment. 您正在使用java:8基本图像,该图像很可能没有提供图形化环境。

You can use ubuntu:18.04 base image with manually installed openjdk-11-jdk and xvfb packages. 您可以将ubuntu:18.04基本映像与手动安装的openjdk-11-jdkxvfb软件包一起使用。 The xvfb-run command will take care of setting up the virtual X Server environment: xvfb-run命令将负责设置虚拟X Server环境:

xvfb-run sets up an X authority file (or uses an existing user-specified one), writes a cookie to it (see xauth(1x)) and then starts the Xvfb X server as a background process. xvfb运行会设置一个X授权文件(或使用现有的用户指定文件),向其中写入一个cookie(请参阅xauth(1x)),然后将Xvfb X服务器作为后台进程启动。 The process ID of Xvfb is stored for later use. Xvfb的进程ID被存储以供以后使用。 The specified command is then run using the X display corresponding to the Xvfb server just started and the X authority file created earlier. 然后,使用与刚启动的Xvfb服务器相对应的X显示器以及之前创建的X授权文件运行指定的命令。

Dockerfile Dockerfile

FROM ubuntu:18.04
RUN apt-get update -y && apt-get upgrade -y && apt-get install -y openjdk-11-jdk xvfb 
ADD JFrameDocker.java MANIFEST.mf ./
RUN javac JFrameDocker.java
RUN jar cfm JFrameDocker.jar MANIFEST.mf JFrameDocker.class 
RUN xvfb-run java -jar JFrameDocker.jar

JFrameDocker.java JFrameDocker.java

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class JFrameDocker {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        JLabel lable = new JLabel("Hello World");
        panel.setLayout(new FlowLayout());  
        frame.add(panel);
        panel.add(lable);
        frame.setSize(800, 600);
        frame.setVisible(true);
        System.out.println("Up and running");
    }

}

MANIFEST.mf MANIFEST.MF

Manifest-Version: 1.0
Main-Class: JFrameDocker

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.awt.AWTError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable - java.awt.AWTError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable jshell:线程“ main”中的异常java.awt.AWTError:无法使用“:0”作为DISPLAY变量的值连接到X11窗口服务器 - jshell : Exception in thread “main” java.awt.AWTError: Can't connect to X11 window server using ':0' as the value of the DISPLAY variable 线程“主”中的异常java.awt.AWTError:无法使用&#39;:0.0&#39;作为DISPLAY变量的值连接到X11窗口服务器 - Exception in thread “main” java.awt.AWTError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable Java 无法使用“localhost:10.0”作为 DISPLAY 变量的值连接到 X11 window 服务器 - Java Can't connect to X11 window server using 'localhost:10.0' as the value of the DISPLAY variable Java:无法使用“ localhost:1.0”作为DISPLAY变量的值连接到X11窗口服务器 - Java: Can't connect to X11 window server using 'localhost:1.0' as the value of the DISPLAY variable 获取 Can't connect to X11 window server using ':0' 作为 DISPLAY 变量的值。 使用 sudo 或 root - obtaining Can't connect to X11 window server using ':0' as the value of the DISPLAY variable. with sudo or root 线程“main”java.awt.AWTError中的异常:BoxLayout无法共享 - Exception in thread “main” java.awt.AWTError: BoxLayout can't be shared 创建BoxLayout时java.awt.AWTERROR - java.awt.AWTERROR When Creating a BoxLayout 无法连接到X11窗口服务器 - Can't connect to X11 window server java.lang.InternalError:无法连接到X11窗口服务器以进行JVisualVM分析会话 - java.lang.InternalError: Can't connect to X11 window server for JVisualVM profiling session
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM