有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Intellij:如何从现有进程更新运行配置环境?

我想从现有/正在运行的进程(从PID中选择)中更新给定运行/调试配置的环境变量

有插件吗?我在jetbrain仓库里找不到这样的东西

有什么足够方便的解决方案(没有变量、值对、手动复制;没有intellij重启)来更新这些环境变量


共 (1) 个答案

  1. # 1 楼答案

    为了获得功能,我写了一个在Linux上运行的bash函数

    idea_run_env()
    {
      typeset rcf=${1}
      if ! [ -f "$rcf" ]; then
          echo "Can not find run configuration file '$rcf'" 1>&2*
          return 2
      fi
      typeset rcf_back=${rcf}.bak
      if (( $# > 1 )); then
        typeset pid=${2}
        # no Esc in attr value, I may/should escape/encode such values but I won't use them
        typeset to_be_bloc=$(xargs -n 1 -0 printf "%s\n" < /proc/$pid/environ | grep -a -v -E $'\e' | while IFS='=' read k v; do echo '<env name="'$k'" value="'$v'" />'; done)
        if [ -z "$to_be_bloc" ] ; then
          echo "Can not find environment for given PID '$pid'" 1>&2*
          return 2
        fi
        #typeset was_block=$(sed -e '/<envs>/,/<\/envs>/{//!b};d' "$rcf")
        typeset prefix=$(sed -e '/<envs>/q' "$rcf")
        typeset postfix=$(sed -n -e '/<\/envs>/,$p' "$rcf")
        [ -f "$rcf_back" ] || mv "$rcf" "$rcf_back"
        echo "${prefix}" > "$rcf"
        echo "${to_be_bloc}" >> "$rcf"
        echo "${postfix}" >> "$rcf"
      elif [ -f "$rcf_back" ]; then
        mv -f  "$rcf_back" "$rcf"
      else
        echo "Can not find backup to restore" 1>&2
        return 1
      fi
    }