我成功地从https://stackoverflow.com/questions/56541688/how-to-run-an-interactive-cli-program-from-within-clojure中实现了一个交互式CLI的工作
但我还需要设置环境变量,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 在类java.lang.ProcessEnvironment$StringEnvironment中找不到匹配的接受2个参数的put方法