我成功地将交互式 CLI 从以下链接运行: https://stackoverflow.com/questions/56541688/how-to-run-an-interactive-cli-program-from-within-clojure
但我也需要设置环境变量,https://stackoverflow.com/questions/30574972/java-processbuilder-and-using-environment-variables 建议使用 pb.environment().put("ENV", "val")
,所以我认为应该是 (-> pb .environment (put. env val))
(defn process-interactive-cli
[args & [working-directory envs]]
(let [process-builder (java.lang.ProcessBuilder. args)
inherit (java.lang.ProcessBuilder$Redirect/INHERIT)
environment (.environment process-builder)]
;; change CWD
(when working-directory
(.directory process-builder (io/file working-directory)))
;; set environment variables
(for [[env val] envs]
(.put environment env val))
(.redirectOutput process-builder inherit)
(.redirectError process-builder inherit)
(.redirectInput process-builder inherit)
;; print the executing command (~ set -x)
(apply println ">" args)
(.waitFor (.start process-builder))))
但调试此代码会得到以下结果
1. 未处理的异常 java.lang.IllegalArgumentException:未找到接受2个参数的 put 方法,所属类为 java.lang.ProcessEnvironment$StringEnvironment