问题描述
系统:Red Hat
前提是只输入pycharm
能正常打开PyCharm,但是用sudo pycharm
会出现如下报错:
$ sudo pycharm
Apr 14, 2022 11:12:13 PM java.util.prefs.FileSystemPreferences$6 run
WARNING: Prefs file removed in background /root/.java/.userPrefs/prefs.xml
2022-04-14 23:12:13,344 [ 176] WARN - llij.ide.plugins.PluginManager - Resource bundle redefinition for plugin 'com.jetbrains.pycharm.community.customization'. Old value: messages.ActionsBundle, new value: messages.PyBundle
MoTTY X11 proxy: Unsupported authorisation protocol
2022-04-14 23:12:13,561 [ 393] ERROR - llij.ide.plugins.PluginManager - UI initialization failed
com.intellij.ide.plugins.StartupAbortedException: UI initialization failed
at com.intellij.idea.StartupUtil.lambda$prepareApp$3(StartupUtil.java:195)
at java.base/java.util.concurrent.CompletableFuture.uniExceptionally(CompletableFuture.java:986)
at java.base/java.util.concurrent.CompletableFuture$UniExceptionally.tryFire(CompletableFuture.java:970)
at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:506)
at java.base/java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:2088)
at com.intellij.idea.StartupUtil.lambda$scheduleInitUi$9(StartupUtil.java:396)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1$1.run(Executors.java:668)
at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1$1.run(Executors.java:665)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1.run(Executors.java:665)
at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.util.concurrent.CompletionException: java.awt.AWTError: Can't connect to X11 window server using 'localhost:10.0' as the value of the DISPLAY variable.
at java.base/java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:331)
at java.base/java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:346)
at java.base/java.util.concurrent.CompletableFuture$BiRelay.tryFire(CompletableFuture.java:1423)
... 10 more
Caused by: java.awt.AWTError: Can't connect to X11 window server using 'localhost:10.0' as the value of the DISPLAY variable.
at java.desktop/sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
at java.desktop/sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:102)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.desktop/sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:61)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:315)
at java.desktop/java.awt.GraphicsEnvironment$LocalGE.createGE(GraphicsEnvironment.java:101)
at java.desktop/java.awt.GraphicsEnvironment$LocalGE.<clinit>(GraphicsEnvironment.java:83)
at java.desktop/java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:129)
at java.desktop/sun.awt.X11.XToolkit.<clinit>(XToolkit.java:233)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:315)
at java.desktop/java.awt.Toolkit$2.run(Toolkit.java:588)
at java.desktop/java.awt.Toolkit$2.run(Toolkit.java:583)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.desktop/java.awt.Toolkit.getDefaultToolkit(Toolkit.java:582)
at java.desktop/java.awt.Toolkit.getEventQueue(Toolkit.java:1499)
at java.desktop/java.awt.EventQueue.invokeLater(EventQueue.java:1318)
at com.intellij.idea.StartupUtil.lambda$scheduleInitUi$9(StartupUtil.java:347)
... 7 more
2022-04-14 23:12:13,565 [ 397] ERROR - llij.ide.plugins.PluginManager - PyCharm 2021.1.2 Build #PC-211.7442.45
2022-04-14 23:12:13,574 [ 406] ERROR - llij.ide.plugins.PluginManager - JDK: 11.0.11; VM: Dynamic Code Evolution 64-Bit Server VM; Vendor: JetBrains s.r.o.
2022-04-14 23:12:13,574 [ 406] ERROR - llij.ide.plugins.PluginManager - OS: Linux
Start Failed
Failed to initialize graphics environment
java.awt.AWTError: Can't connect to X11 window server using 'localhost:12.0' as the value of the DISPLAY variable.
解决方法
cd ~
vim .bashrc
将xhost + local:all
添加到 export的后面一行,PATH的前面一行
之后:wq
保存
然后终端输入env |grep DISPLAY,将结果复制
$ env |grep DISPLAY
DISPLAY=localhost:12.0
然后寻找 pycharm.sh 文件
bgone@federo~
$ which pycharm
/usr/local/bin/pycharm
bgone@federo~
$ cd /usr/local/bin/
bgone@federo /usr/local/bin
$ ll |grep pycharm
lrwxrwxrwx 1 root root 46 Apr 14 12:19 pycharm -> /opt/pycharm-community-2021.1.2/bin/pycharm.sh
bgone@federo /usr/local/bin
$ cd /opt/pycharm-community-2021.1.2/bin
bgone@federo /opt/pycharm-community-2021.1.2/bin
$ vim pycharm.sh
将export DISPLAY=localhost:12.0
添加到脚本开头,没有权限要先授权;保存关闭后,
打开一个终端输入pycharm,出现pycharm窗口后,再开一个终端,输入sudo pycharm
,会再出现一个窗口,把两个都关掉,重启之后用sudo pycharm
就能打开pycharm了
如果还有问题,可以留言或私信,并把问题描述清楚。