java.awt.awterror,java.awt.AWTError: Can't connect to X11 window server using ':0' as the value of t..._valp的博客-程序员秘密

技术标签: java.awt.awterror  

问题

I have been trying to run a Java AWT based application on an Ubuntu VM inside Docker. The application is a very simple one and it flawlessly runs on Windows through Eclipse. 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”.

However the story is not same when I try to run it inside 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” . 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. 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.

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:

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 Build : sudo docker build -t jframedocker .

Docker Run : sudo docker run jframedocker

Output Without Error Screenshot: (Without Docker)

Output with Docker:

[email protected]:~/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.(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.(Window.java:537)

at java.awt.Frame.(Frame.java:420)

at java.awt.Frame.(Frame.java:385)

at javax.swing.JFrame.(JFrame.java:189)

at com.etp.JFrameDockerTest.main(JFrameDockerTest.java:12)

... 5 more

回答1:

You are using java:8 base image which most likely doesn't provide a graphical environment.

You can use ubuntu:18.04 base image with manually installed openjdk-11-jdk and xvfb packages. The xvfb-run command will take care of setting up the virtual X Server environment:

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. The process ID of Xvfb is stored for later use. The specified command is then run using the X display corresponding to the Xvfb server just started and the X authority file created earlier.

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

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-Version: 1.0

Main-Class: JFrameDocker

来源:https://stackoverflow.com/questions/55220901/java-awt-awterror-cant-connect-to-x11-window-server-using-0-as-the-value-of

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42520780/article/details/115784361

智能推荐

win7锁定计算机后怎么打开,如何在win7电脑中设置密码输入三次就锁定电脑?_JMLLJM的博客-程序员秘密

咱们为自己的win7电脑设置一个开机密码,自然是因为不想让人随意的动用咱们的电脑,或者说在咱们的win7电脑中存放有一些比较重要的文件,或者说不想让别人随意的就可以使用自己的电脑。那么单凭一个开机密码真的安全吗?若是身边熟悉自己的人,是不是就掌握了打开咱们win7电脑的线索呢?或者说,人家多试几次是不是就可以打开咱们的电脑呢?如果你还在担心这个的话,小编今天就来介绍一个技巧,让win7电脑的开机密...

AEM集成SPA(一)架构及概念梳理_SerikaOnoe的博客-程序员秘密

前言这篇文章主要是对AEM前沿技术文档的研究,主要整理了Adobe Experience Management官方文档对于集成SPA框架的各类文档,并加以略微梳理,本篇文章适合有一定AEM开发经验和SPA框架开发经验的人阅读。这篇文章内容主要是概念性的内容,在对英文文档翻译的过程中,部分深奥的概念我只能借助图来理解,因此翻译可能会有点小出入。读者只需要略微了解即可,下一篇文章将给出AEM集成React框架的完整教程。文章目录前言1 名词解释2 AEM混合架构2.1 架构对比2.2 Content Fr

什么是程序_Seloman的博客-程序员秘密_什么是程序

什么是程序?1.(从构成上看)计算机只能一个个地处理简单任务,这种简单任务叫做指令,指令的有序组织就是程序。2.(从性质与目的看)人们为了使用计算机解决某种问题,用计算机可以识别的代码编排的一系列数据处理过程便是程序。什么是程序设计?1.按照特定规则,编排计算机能识别的特定的指令组合的过程。什么是程序设计语言?1.方便人们程序设计而提供的一种手段。机器语言的缺点编写机器语言是一件痛苦的事情,人们直接用计算机能识别的0 1代码,来编写程序,并且不同的计算机指令系统是不一样的,这

Android烧录镜像文件介绍_TseJung的博客-程序员秘密_rpm.mbn

sbl1.mbn烧录命令:fastboot flash sbl1 sbl1.bin作用:second bootloader1的缩写,是在little kernel(lk)前启动,起到引导lk的作用,如果将该分区擦除,则设备表现为进入紧急下载模式,即,擦除该分区后,插入USB显示QDload端口。rpm.mbn烧录命令:fastboot flash rpm rpm.mbn作用:电源管理器,...

Matplotlib还能这么画!(附25个案例详细代码)_数据不吹牛的博客-程序员秘密

作者:zsx_yiyiyi编辑:python大本营大家好,今天分享给大家25个Matplotlib图的汇总,在数据分析和可视化中非常有用,文章较长,可以收藏下来慢慢练手。#!pipi...

微信小程序-ColorUI的demo错误_BeYourOwnHero的博客-程序员秘密

#form表单里,图片每次选择的限制为四个,当已经传了三张图片时,会继续可以选择四张图片,这样就会显示出7张图片,可以尝试如下修改:ChooseImage() { var maxImgNum = 4-this.data.product.pictures.length wx.chooseImage({ count: maxImgNum, //默认9 siz...

随便推点

Java线程池的使用方法_ShyTan的博客-程序员秘密_java使用线程池

前言多线程的异步执行方式,虽然能够最大限度发挥多核计算机的计算能力,但是如果不加控制,反而会对系统造成负担。线程本身也要占用内存空间,大量的线程会占用内存资源并且可能会导致Out of Memory。即便没有这样的情况,大量的线程回收也会给GC带来很大的压力。为了避免重复的创建线程,线程池的出现可以让线程进行复用。通俗点讲,当有工作来,就会向线程池拿一个线程,当工作完成后,并不是直接关闭线程,而是将这个线程归还给线程池供其他任务使用。接下来从总体到细致的方式,来共同探讨线程池。总体的架构

全国各省市 残疾人联合会 就业 康复 网站导航_iteye_8122的博客-程序员秘密

原文地址:全国各省市<wbr>残疾人联合会<wbr>就业<wbr>康复<wbr>网站导航</wbr></wbr></wbr></wbr>作者:黄理彪一、残疾人联合会网中国残疾人联合会<wbr><wbr>&

解决json_encode中文UNICODE转码问题_daxia_85的博客-程序员秘密

[使用场景1]  我们将数据经过json_encode()处理后,存进表中由于, 用PHP的json_encode来处理中文的时候, 中文都会被编码, 变成不可读的, 类似”\u***”的格式,如下所示:$arr = ['name' => '大霞', 'sex'=> 'f'];$str = json_encode($arr);

python selenium隐式等待和显示等待_LLLLLLLLLLLLIU的博客-程序员秘密_python selenium隐式等待

from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import By"""### 隐式等待和显示等待1. 隐式等待:指定一个时间,在这个时间内一直会处于等待状态。隐式等待需要使用

U盘在任务栏有显示,但是在我的电脑里面没显示_江州山农的博客-程序员秘密

导读第一次遇到这种情况:U盘在别人电脑上都好好的,但是是在自己电脑上无法找到盘符,而USB的识别信息是都有的。参考资料:百度知道#解决方案先插上U盘,然后右键我的电脑-属性-设备管理器然后找到U盘所在的那一栏,然后右键U盘的名字-卸载-确定之后拔出U盘,再插上,就会正常显示出来。是不是很简单。...

图像识别_邓文(desaco)的博客-程序员秘密_图像识别方法主要有三种

图像识别主要用到了两个第三方的iOS框架:OpenCV和TesseractOCR,OpenCV用来做图像处理,定位到身份证号码的区域,TesseractOCR则是对定位到的区域内的内容进行识别。 OpenCV中的一些简单的处理图像的方法:灰度处理、二值化、腐蚀、边缘检测等等。Tesseract Open Source OCR Engine (main repository)- http...

推荐文章

热门文章

相关标签