启动没有图形环境的Java应用程序

时间:2017-08-06 10:46:25

标签: docker

当我从docker中运行我的应用程序时,我收到以下Java异常。

Exception in thread "AWT-EventQueue-0" java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
        at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:204)
        at java.awt.Window.<init>(Window.java:536)
        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 mantra.mfs100.test.MFS100Test.<init>(MFS100Test.java:67)
        at mantra.mfs100.test.MFS100Test$8.run(MFS100Test.java:450)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
        at java.awt.EventQueue.access$500(EventQueue.java:97)
        at java.awt.EventQueue$3.run(EventQueue.java:709)
        at java.awt.EventQueue$3.run(EventQueue.java:703)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我的docker文件看起来像这样......

# cat Dockerfile
FROM openjdk:8
COPY . /usr/src/myapp
WORKDIR "x64/mfs100_9.0.2.2_x86_redist/"
RUN "./install.sh"

WORKDIR /usr/src/myapp

CMD ["java", "-jar", "/usr/src/myapp/dist/myap.jar"]

我正在使用亚马逊提供的Ubuntu桌面进行测试。如果我在Ubuntu桌面上安装Java应用程序(没有docker),它会按预期工作。这个docker问题还是我访问Amazon cloud ubuntu的方式?

2 个答案:

答案 0 :(得分:1)

你应该设置无头模式,传递java.awt.headless=true,使用类似的东西:

CMD ["java", "-Djava.awt.headless=true", "-jar", "/usr/src/myapp/dist/myap.jar"]

答案 1 :(得分:1)

有多种方法可以做到这一点。最常见的方法是将主机X11显示共享到docker容器

docker run -d \
    -v /tmp/.X11-unix:/tmp/.X11-unix \
    -v /etc/localtime:/etc/localtime \
    -e DISPLAY=unix$DISPLAY \
    <yourcontainer>

接下来是@SimonlucaLandi,建议你可以运行无头版

CMD ["java", "-Djava.awt.headless=true", "-jar", "/usr/src/myapp/dist/myap.jar"]

如果您需要使用X11显示版

,下面是一些您可以参考的更多相关参考资料

How to view GUI apps from inside a docker container

X11 forwarding of GUI app in Docker container

https://medium.com/@dimitris.kapanidis/running-gui-apps-in-docker-containers-3bd25efa862a