V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
XhstormR
V2EX  ›  问与答

Java 启动一个外部程序后如何在与其交互的同时,又能阻塞主线程呢?

  •  
  •   XhstormR · 2016-09-15 20:07:02 +08:00 · 1760 次点击
    这是一个创建于 2788 天前的主题,其中的信息可能已经有所发展或是发生改变。
    private static void mergeFLV() throws IOException, InterruptedException {
        String string = "cmd.exe /C start D:/Download/ffmpeg.exe -f concat -i D:/Download/123.txt -c copy D:/Download/test.flv";
        Process exec = Runtime.getRuntime().exec(string);
        exec.waitFor();
        System.out.println("OK!!!");
    }
    

    如果是 cmd.exe /C start ,可以显示 ffmpeg.exe ,但 cmd.exe 直接关闭了,就直接打印 OK 了。

    如果是 cmd.exe /K start ,可以显示 ffmpeg.exe ,但关闭 ffmpeg.exe 后, cmd.exe 没有关闭,在后台一直留着,就一直阻塞着。

    最好的是 cmd.exe /C,不显示 ffmpeg.exe 的窗口,在任务管理器关掉 ffmpeg 后, cmd.exe 也直接关闭了,就打印 OK 了。就是不知道怎么显示 ffmpeg.exe 的窗口与其交互。

    搞了一下午了,就想造个轮子,解析+下载+合并 bilibili 的超清视频,就差最后一步了, 555555

    7 条回复    2016-09-15 22:30:13 +08:00
    oott123
        1
    oott123  
       2016-09-15 20:36:05 +08:00
    为啥要 cmd 呢?
    直接启动 ffmpeg.exe 不就好了……
    XhstormR
        2
    XhstormR  
    OP
       2016-09-15 20:48:39 +08:00
    @oott123 刚试了下,可以运行,但是不会显示 ffmpeg.exe 的窗口,怎么才能显示 ffmpeg.exe 的窗口啊。
    oott123
        3
    oott123  
       2016-09-15 21:00:43 +08:00
    http://stackoverflow.com/questions/8149828/read-the-output-from-java-exec

    试试这个。我理解其实你不一定要窗口出来,拿到输出就可以了。
    SoloCompany
        4
    SoloCompany  
       2016-09-15 21:47:04 +08:00
    去掉 start
    XhstormR
        5
    XhstormR  
    OP
       2016-09-15 22:19:46 +08:00
    @SoloCompany 去掉 start ,就没有 ffmpeg.exe 的窗口了。
    SoloCompany
        6
    SoloCompany  
       2016-09-15 22:23:48 +08:00   ❤️ 1
    @XhstormR 那就 start /w 或 start /wait
    XhstormR
        7
    XhstormR  
    OP
       2016-09-15 22:30:13 +08:00
    @SoloCompany 感谢,有效果,加个 /w 就好了,我晕啊。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2588 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 08:41 · PVG 16:41 · LAX 01:41 · JFK 04:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.