尝试使用swing和frame将javagui应用程序作为docker映像运行

nbnkbykc  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(236)

我有一个java应用程序,它连接到我之前设置的AmazonEC2服务器,该服务器使用swing和AWTJava库在FrameGUI中运行协作白板应用程序,并在端口8888上使用套接字编程。这个应用程序工作得很好,但我正在努力让它作为一个码头形象的工作。我已经将项目导出为一个名为“col\u whiteboard.jar”的.jar文件,并且我要运行的jar文件中的类名为“drawcanvas”。这是与.jar文件位于同一目录中的dockerfile:

FROM azul/zulu-openjdk-alpine:11 as packager
WORKDIR ./whiteboard
COPY col_whiteboard.jar .
EXPOSE 8888
ENV DISPLAY =:0.0
CMD ["java","DrawCanvas"]
ENTRYPOINT ["java","-jar","col_whiteboard.jar"]

这是我的生成命令:

$ docker build -t collab_board .

以及我的run命令:

$ docker run -p 8888 collab_board

当我运行命令时,我在一个ubuntu20.04虚拟机中。
我得到的错误如下:

DrawCanvaException in thread "main" java.awt.AWTError: Can't connect to X11 window server using '=:0.0' as the value of the DISPLAY variable.

请帮助我了解出了什么问题,以及如何解决它!我已经被困在这个问题上好几天了,如果这是一个愚蠢的问题,我道歉,我对docker和JavaGUI应用程序非常陌生。
更新:我已经解决了x11的问题,通过使用命令允许所有用户访问xhost xhost + 在终端中,使用run命令:

docker run --net=host --env="DISPLAY" --volume="$HOME/.Xauthority:/root/.Xauthority:rw" collab_board

但是,容器仍不运行,出现以下错误:

Exception in thread "main" java.lang.InternalError: java.lang.reflect.InvocationTargetException
    at java.desktop/sun.font.FontManagerFactory$1.run(FontManagerFactory.java:86)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.desktop/sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74)
    at java.desktop/sun.font.SunFontManager.getInstance(SunFontManager.java:249)
    at java.desktop/sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:265)
    at java.desktop/sun.swing.SwingUtilities2.getFontMetrics(SwingUtilities2.java:1243)
    at java.desktop/javax.swing.JComponent.getFontMetrics(JComponent.java:1646)
    at java.desktop/javax.swing.plaf.basic.BasicLabelUI.getPreferredSize(BasicLabelUI.java:246)
    at java.desktop/javax.swing.JComponent.getPreferredSize(JComponent.java:1680)
    at java.desktop/java.awt.FlowLayout.layoutContainer(FlowLayout.java:609)
    at java.desktop/java.awt.Container.layout(Container.java:1537)
    at java.desktop/java.awt.Container.doLayout(Container.java:1526)
    at java.desktop/java.awt.Container.validateTree(Container.java:1722)
    at java.desktop/java.awt.Container.validateTree(Container.java:1731)
    at java.desktop/java.awt.Container.validateTree(Container.java:1731)
    at java.desktop/java.awt.Container.validateTree(Container.java:1731)
    at java.desktop/java.awt.Container.validate(Container.java:1657)
    at java.desktop/java.awt.Container.validateUnconditionally(Container.java:1694)
    at java.desktop/java.awt.Window.show(Window.java:1050)
    at java.desktop/java.awt.Component.show(Component.java:1716)
    at java.desktop/java.awt.Component.setVisible(Component.java:1663)
    at java.desktop/java.awt.Window.setVisible(Window.java:1031)
    at collab_whiteboard.DrawCanvas.<init>(DrawCanvas.java:71)
    at collab_whiteboard.DrawCanvas.main(DrawCanvas.java:422)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at java.desktop/sun.font.FontManagerFactory$1.run(FontManagerFactory.java:84)
    ... 23 more
Caused by: java.lang.NullPointerException
    at java.desktop/sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1262)
    at java.desktop/sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:225)
    at java.desktop/sun.awt.FontConfiguration.init(FontConfiguration.java:107)
    at java.desktop/sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:719)
    at java.desktop/sun.font.SunFontManager$2.run(SunFontManager.java:379)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.desktop/sun.font.SunFontManager.<init>(SunFontManager.java:324)
    at java.desktop/sun.awt.FcFontManager.<init>(FcFontManager.java:35)
    at java.desktop/sun.awt.X11FontManager.<init>(X11FontManager.java:56)
    ... 28 more
flvlnr44

flvlnr441#

我刚刚通过使用更新的run命令解决了这个问题,并将dockerfile更改为以下内容:

FROM ubuntu:latest
RUN \
apt-get update -y && \
apt-get install default-jre -y
ADD collab_whiteboard.jar whiteboardsys.jar
CMD java -jar whiteboardsys.jar

相关问题