From 8da121961b48ce3eb6607767fc1e7864c1b1f4cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E5=87=AF=E6=B3=A2?= <1069977596@qq.com> Date: Fri, 16 Aug 2019 12:59:37 +0800 Subject: [PATCH] =?UTF-8?q?java=E4=BB=A3=E7=A0=81fork=E5=AD=90=E8=BF=9B?= =?UTF-8?q?=E7=A8=8B=EF=BC=88=E6=94=B9=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/github/hcsp/shell/Fork.java | 8 ++++++++ 1 file changed, 8 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..3b749d3 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,13 @@ 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() {