From 55b28d8c017a909c68e1a20c527979f04a9f3e3e Mon Sep 17 00:00:00 2001 From: niuweizhe Date: Mon, 28 Dec 2020 21:54:09 +0800 Subject: [PATCH] java fork process --- 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..dde3333 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 env = pb.environment(); + env.put("AAA", "123"); + pb.redirectOutput(getOutputFile()); + pb.start().waitFor(); } private static File getWorkingDir() {