java.io.IOException: Cannot run program "echo": CreateProcess error=2, ?????????
publicvoidcallCmd(){StringfileName="a.txt";Stringsplicing="hahahahha";//将异常数据同时打印到控制台...
public void callCmd() {
String fileName = "a.txt";
String splicing = "hahahahha";
//将异常数据同时打印到控制台上
System.out.println("echo "+ splicing +" >> ./"+fileName);
//System.out.println(splicing());
Runtime runtime=Runtime.getRuntime();
try{
Process process = runtime.exec("echo "+ splicing +" >> ./"+fileName);
InputStream in = process.getInputStream();
while (in.read() != -1) {
System.out.println(in.read());
}
in.close();
process.waitFor();
}catch(Exception e){
System.out.println("写入到文件"+fileName+"失败!");
e.printStackTrace();
}
}
控制台输出:
echo hahahahha >> ./a.txt
复制后在cmd中能正常执行。说明语句应该是没错的。
求大神指导!
报错消息(部分):
java.io.IOException: Cannot run program "echo": CreateProcess error=2, ?????????
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
....
Caused by: java.io.IOException: CreateProcess error=2, ?????????
at java.lang.ProcessImpl.create(Native Method) 展开
String fileName = "a.txt";
String splicing = "hahahahha";
//将异常数据同时打印到控制台上
System.out.println("echo "+ splicing +" >> ./"+fileName);
//System.out.println(splicing());
Runtime runtime=Runtime.getRuntime();
try{
Process process = runtime.exec("echo "+ splicing +" >> ./"+fileName);
InputStream in = process.getInputStream();
while (in.read() != -1) {
System.out.println(in.read());
}
in.close();
process.waitFor();
}catch(Exception e){
System.out.println("写入到文件"+fileName+"失败!");
e.printStackTrace();
}
}
控制台输出:
echo hahahahha >> ./a.txt
复制后在cmd中能正常执行。说明语句应该是没错的。
求大神指导!
报错消息(部分):
java.io.IOException: Cannot run program "echo": CreateProcess error=2, ?????????
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
....
Caused by: java.io.IOException: CreateProcess error=2, ?????????
at java.lang.ProcessImpl.create(Native Method) 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询