[英]Runtime.exec command not working
我有一个java应用程序,使用wget从Web服务下载文件。 当通过java执行命令时,它返回:“wget:not a http或ftp url:”当我直接执行命令时,它运行没有问题。 这是我的代码:
try {
Debug.println("Starting copy of "+srcFile+" to "+destFile);
String command = "wget -O " + destFile + " \""+ srcFile +"\"";
Process p = Runtime.getRuntime().exec(command);
int exitCode = p.waitFor();
if(Debug.isDebugMode())
{
Debug.println(command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String s;
while((s = stdInput.readLine()) != null)
{
Debug.println(s);
}
}
Debug.println("Finished with code: " + String.valueOf(exitCode));
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
这是输出:
24/04/2013 10:11:05 Starting copy of stoppenmetroken.webcolors.local/service/track?track=3b1ac68a288345c183a08c714901a398&mac=089000A09090 to /opt/byato/data/song/3b1ac68a288345c183a08c714901a398
24/04/2013 10:11:05 wget -O /opt/byato/data/song/3b1ac68a288345c183a08c714901a398 "stoppenmetroken.webcolors.local/service/track?track=3b1ac68a288345c183a08c714901a398&mac=089000A09090"
24/04/2013 10:11:05 wget: not an http or ftp url: "http://stoppenmetroken.webcolors.local/service/track?track=3b1ac68a288345c183a08c714901a398&mac=089000A09090"
24/04/2013 10:11:05 Finished with code: 1
ps:我删除了http://部分输出,因为我没有足够的声望点 - .-
我错过了什么?
你能尝试执行这样的命令:
进程p = Runtime.getRuntime()。exec(“/ bin / bash -c”+ command); //对于linux
要么
进程p = Runtime.getRuntime()。exec(“cmd.exe / c”+ command); //对于Windows
有时我们需要显式调用Linux shell或命令提示符。
希望这会奏效。
我怀疑这个:
String command = "wget -O " + destFile + " \""+ srcFile +"\"";
是问题。 在shell中运行时,将删除URL周围的引号。 但是,当您通过Java运行时,您没有通过shell运行,并且您的URL以"http...
(仔细查看错误消息) "http...
开头。
如果您不希望Runtime.exec() t
解析并拆分您的参数,那么您可能会考虑采用单个参数的变体 。 更有效的解决方案仍然是使用HttpComponents下载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.