我使用此代码在我的java程序中执行脚本
pb.environment().put("time", time);
pb.environment().put("value", value);
Process p = pb.start();
p.waitFor();
// get the return value
等待进程后是否可以获得输出值?输出值是指脚本中使用的回声值
答案 0 :(得分:0)
尝试这样的事情;
test.sh
#!/bin/bash
echo $time
echo $value
java class;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class RunShellScript {
public static void main(String[] args) {
String command = "/tmp/test.sh";
try {
ProcessBuilder pb = new ProcessBuilder(command);
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String time = sdf.format(cal.getTime());
String value = "your Value";
pb.environment().put("time", time);
pb.environment().put("value", value);
Process p = pb.start();
p.waitFor();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(
p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(
p.getErrorStream()));
// to standard output
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// to standard error
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
java控制台输出:
16:03:03
your Value