From 0adbf8805f6bc3a14b3ac73bf99cbb320f8729f0 Mon Sep 17 00:00:00 2001 From: zaker-666 <1075151259china@gmail.com> Date: Sun, 5 Jan 2020 10:31:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86fork?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/github/hcsp/shell/Fork.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/com/github/hcsp/shell/Fork.java b/src/main/java/com/github/hcsp/shell/Fork.java index 28e0700..49707e2 100644 --- a/src/main/java/com/github/hcsp/shell/Fork.java +++ b/src/main/java/com/github/hcsp/shell/Fork.java @@ -3,6 +3,7 @@ import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Map; public class Fork { public static void main(String[] args) throws Exception { @@ -10,6 +11,12 @@ public static void main(String[] args) throws Exception { // 工作目录是项目目录下的working-directory目录(可以用getWorkingDir()方法得到这个目录对应的File对象) // 传递的命令是sh run.sh // 环境变量是AAA=123 + ProcessBuilder pb = new ProcessBuilder("sh", "run.sh"); + pb.directory(getWorkingDir()); + Map envs = pb.environment(); + envs.put("AAA", "123"); + pb.redirectOutput(getOutputFile()); + pb.start().waitFor(); } private static File getWorkingDir() {