Intellij 终端的行为不同于 Ubuntu 终端的 pulseaudio/pacmd/pactrl 命令

Intellij Terminal behaving differently than Ubuntu Terminal for pulseaudio/pacmd/pactrl commands

我正在尝试 运行 在 Java 中使用 ProcessBuilder 进行一些 pulseaudio 操作,例如pacmd list-source-outputs 于 Ubuntu 18.04。当我 运行 直接来自 Intellij 的代码时,它说 No PulseAudio daemon running, or not running as session daemon. 但是,如果我转到 build/classes/java/main 并执行 java MyMainClass 它会按预期工作。

我认为这与 Intellij 终端的集成方式有关。它的行为似乎与 OS 终端不同(见图)。有人对 Intellij 终端有更多见解吗?

Process p = null;
try {
    p = new ProcessBuilder("pacmd", "list-source-outputs").start();

    printStream(p.getInputStream());
    printStream(p.getErrorStream());

    p.waitFor();
} catch (IOException | InterruptedException e) {
    e.printStackTrace();
}

编辑:我的终端设置:

问题出在 pulseaudio 上。对我有用的是打电话

export PULSE_RUNTIME_PATH=/run/user/1000/pulse

在 运行 任何 pulseaudio/pacmd/pacntl 命令之前。导出命令doesn't seem to work from runtime。但是,您可以创建一个 shell 文件,然后执行您的命令:

test.sh:

#!/bin/bash
export PULSE_RUNTIME_PATH=/run/user/1000/pulse
pacmd list-source-outputs

在Java中:

Runtime.getRuntime().exec("sh test.sh");

更容易处理动态调用:

test.sh:

#!/bin/bash
export PULSE_RUNTIME_PATH=/run/user/1000/pulse

在Java中:

Runtime.getRuntime().exec("sh test.sh \"pacmd list-source-outputs\"");

这里是 Jetbrains 的官方回答:

How do you launch IDE: from terminal via .sh script or from desktop launcher? make sure to try to launch it from terminal via .sh script.

Also try restarting the IDE after starting the pulseaudio or restart the pulseaudio daemon in IDE terminal. Try also these suggestions: https://bbs.archlinux.org/viewtopic.php?pid=1214072#p1214072 https://bbs.archlinux.org/viewtopic.php?pid=1214157#p1214157

Note that after changing the environment it might be needed to restart the IDE.