您正在连续执行三个命令。每个命令应该是一个单独的Process
. 此外,命令和参数应分解为数组的元素:
Process send1 = Runtime.getRuntime().exec(new String[] {"javac", "/tmp/"+ fileName});
send1.waitFor(); // this returns an int with the exit status of the command - you really should check this!
Process send2 = Runtime.getRuntime().exec(new String[] {"sed", "-i", "s/Foo/Foo2/g", "/tmp/"+ fileName});
send2.waitFor();
Process send3 = Runtime.getRuntime().exec(new String[] {"java", "/tmp/"+ fileNameShort+".class"});
send3.waitFor();
或者,将整个内容提供给sh -c
(尽管您确实应该使用以前的方法,因为这样您就不必担心转义参数等)
Process send = Runtime.getRuntime().exec(new String[] {"sh", "-c", "javac /tmp/"+ fileName + "; sed -i 's/Foo/Foo2/g' /tmp/"+ fileName + "; java /tmp/"+ fileNameShort + ".class"});