From a3e4e85ac2036ce9a7c1266b95bd9a47c7ca41e2 Mon Sep 17 00:00:00 2001 From: Ethan Wang <1527185002@qq.com> Date: Mon, 2 Sep 2019 21:02:20 +0800 Subject: [PATCH 1/2] =?UTF-8?q?forkzi=E5=AD=90=E8=BF=9B=E7=A8=8B?= 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..858a812 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.start().waitFor(); + } private static File getWorkingDir() { From 62bfc88e34256a9b0ec915421d9805a71a00910f Mon Sep 17 00:00:00 2001 From: Ethan Wang <1527185002@qq.com> Date: Mon, 2 Sep 2019 21:05:29 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fork=E5=AD=90=E8=BF=9B=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/github/hcsp/shell/Fork.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/github/hcsp/shell/Fork.java b/src/main/java/com/github/hcsp/shell/Fork.java index 858a812..56d15ba 100644 --- a/src/main/java/com/github/hcsp/shell/Fork.java +++ b/src/main/java/com/github/hcsp/shell/Fork.java @@ -15,6 +15,7 @@ public static void main(String[] args) throws Exception { pb.directory(getWorkingDir()); Map envs = pb.environment(); envs.put("AAA","123"); + pb.redirectOutput(getOutputFile()); pb.start().waitFor(); }