简体   繁体   English

Runtime.exec命令无法正常工作

[英]Runtime.exec command not working

I have a java application that downloads a file from a web service using wget. 我有一个java应用程序,使用wget从Web服务下载文件。 When executing the command through java it returns with: "wget: not an http or ftp url:" When i execute the command directly it runs without problems. 当通过java执行命令时,它返回:“wget:not a http或ftp url:”当我直接执行命令时,它运行没有问题。 Here is my code: 这是我的代码:

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;

And this is the output: 这是输出:

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: i removed the http:// part of the output because i dont have enough reputation points -.- ps:我删除了http://部分输出,因为我没有足够的声望点 - .-

What am i missing? 我错过了什么?

Can you try to execute the command like this : 你能尝试执行这样的命令:

Process p = Runtime.getRuntime().exec("/bin/bash -c "+command); 进程p = Runtime.getRuntime()。exec(“/ bin / bash -c”+ command); //for linux //对于linux

or 要么

Process p = Runtime.getRuntime().exec("cmd.exe /c "+command); 进程p = Runtime.getRuntime()。exec(“cmd.exe / c”+ command); //for Windows //对于Windows

Sometimes we need to explicitly invoke Linux shell or command prompt. 有时我们需要显式调用Linux shell或命令提示符。

Hope this will work. 希望这会奏效。

I suspect this: 我怀疑这个:

String command = "wget -O " + destFile + " \""+ srcFile +"\"";

is the problem. 是问题。 When you run in a shell, the quotes around the URL will be removed. 在shell中运行时,将删除URL周围的引号。 However when you run via Java you're not running via a shell and your URL starts with "http... (look closely at the error message). 但是,当您通过Java运行时,您没有通过shell运行,并且您的URL以"http... (仔细查看错误消息) "http...开头。

If you don't want Runtime.exec() t o parse and split your arguments then you might consider the variant that takes individual arguments . 如果您不希望Runtime.exec() t解析并拆分您的参数,那么您可能会考虑采用单个参数的变体 A more efficient solution still would be to download using HttpComponents . 更有效的解决方案仍然是使用HttpComponents下载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM