那你可以把命令分开执行 String[] cmds = { "echo #/bin/bash > tempExec.sh", "echo java xxx \" \" param2... > > tempExec.sh", "chmod 755 tempExec.sh" } Runtime.getRuntime().exec(cmds);String cmd = "./tempExec.sh"; Process p = Runtime.getRuntime().exec(cmd); //do something herecmd = "rm tempExec.sh"; Process p = Runtime.getRuntime().exec(cmd);
还是不行,试了一下,根本通过这种命令产生不了shell脚本。
应该不会产生不了脚本的,可能是时间问题 String[] cmds = { "echo #/bin/bash > tempExec.sh", "echo java xxx \" \" param2... > > tempExec.sh", "chmod 755 tempExec.sh" } Process p = Runtime.getRuntime().exec(cmds); //这里有必要等待 p.waitFor();String cmd = "./tempExec.sh"; Process p = Runtime.getRuntime().exec(cmd); //do something here //这里也有必要等待cmd = "rm tempExec.sh"; Process p = Runtime.getRuntime().exec(cmd); //等待的时候注意死锁,应该要手工清空p.getInputStream()的buffer,那要写一个线程了,好像问题就复杂化了 以下是javadoc里的一段话,倒数一句说明了死锁的理由。 The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts. The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock或者你用某个特殊符号代替空格参数,然后在java里进行判断,如果是这个特殊符号的就转成空格处理。这样会不会好一些。
String cmd = "java your_other_class \" \" your_param2...";
System.out.println(cmd);
Runtime.getRuntime().exec(cmd);
可不可以这样,先写到临时文件,执行完了再删除临时文件
String[] cmds = {
"echo #/bin/bash > tempExec.sh",
"echo java xxx \" \" param2... >> tempExec.sh",
"chmod 755 tempExec.sh",
"./tempExec.sh",
"rm tempExec.sh"
};
Runtime.getRuntime().exec(cmds);
String[] cmds = {
"echo #/bin/bash > tempExec.sh",
"echo java xxx \" \" param2... > > tempExec.sh",
"chmod 755 tempExec.sh"
}
Runtime.getRuntime().exec(cmds);String cmd = "./tempExec.sh";
Process p = Runtime.getRuntime().exec(cmd);
//do something herecmd = "rm tempExec.sh";
Process p = Runtime.getRuntime().exec(cmd);
String[] cmds = {
"echo #/bin/bash > tempExec.sh",
"echo java xxx \" \" param2... > > tempExec.sh",
"chmod 755 tempExec.sh"
}
Process p = Runtime.getRuntime().exec(cmds);
//这里有必要等待
p.waitFor();String cmd = "./tempExec.sh";
Process p = Runtime.getRuntime().exec(cmd);
//do something here
//这里也有必要等待cmd = "rm tempExec.sh";
Process p = Runtime.getRuntime().exec(cmd); //等待的时候注意死锁,应该要手工清空p.getInputStream()的buffer,那要写一个线程了,好像问题就复杂化了
以下是javadoc里的一段话,倒数一句说明了死锁的理由。
The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts. The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock或者你用某个特殊符号代替空格参数,然后在java里进行判断,如果是这个特殊符号的就转成空格处理。这样会不会好一些。