刚接触 Ubuntu 16,在终端中运行以下代码时出现此异常。我应该怎么做才能修复它?
Exception in thread "main" java.awt.AWTError: Can't connect to X11 window server using ':0.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 sun.awt.X11.XToolkit.<clinit>(XToolkit.java:126)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at java.awt.Toolkit$2.run(Toolkit.java:860)
at java.awt.Toolkit$2.run(Toolkit.java:855)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:854)
at javax.swing.ImageIcon.<init>(ImageIcon.java:198)
at oop.ex2.GameGUI.createImageIcon(GameGUI.java:188)
at oop.ex2.GameGUI.<clinit>(GameGUI.java:19)
at SpaceShipFactory.createSpaceShips(SpaceShipFactory.java:8)
at SpaceWars.createSpaceShips(SpaceWars.java:77)
at SpaceWars.<init>(SpaceWars.java:55)
at SpaceWars.main(SpaceWars.java:264)
编辑:我尝试插入以下代码并收到新错误:
/usr/bin/java -Djava.awt.headless=true ~oop/bin/ex2/SpaceWars h d
Error: Could not find or load main class
.cs.course.current.oop.bin.ex2
提前致谢!
答案1
在终端中运行此命令,从而允许连接到 X11
xhost +
安装或运行完游戏后
xhost -
...出于安全原因。
答案2
@GregH747 给出的命令和下面的命令对我有用,至少在 Fedora 中是这样。不知道 Ubuntu 的情况,因为我从未在 Ubuntu 中遇到过。
:~$ xhost +
打开 $HOME/.bash_profile
JAVA_OPTIONS=-Djava.awt.headless=true
export JAVA_OPTIONS
然后
:~$ source ~/.profile
然后再次运行你的命令或应用程序