简体   繁体   English

Runtime.getRuntime().exec(command) 不起作用

[英]Runtime.getRuntime().exec(command) doesn't work

I have a problem with executing command in Runtime.getRuntime().exec(command) on my linux DEV machine.我在 linux DEV 机器上执行 Runtime.getRuntime().exec(command) 中的命令时遇到问题。 I'm trying to convert html content to mobi format using calibre but it doesn't work.我正在尝试使用 calibre 将 html 内容转换为 mobi 格式,但它不起作用。 Even if I added log.warn to display what this process returns it shows nothing.即使我添加了 log.warn 来显示此进程返回的内容,它也什么也没显示。

It works on my local machine where I do have Windows 10 and I read on the Inte.net that I should point out that command should be launched on linux so I added String[] cmd = {"bash", "-c", command};它在我的本地机器上工作,我有 Windows 10 我在 Inte.net 上读到我应该指出该命令应该在 linux 上启动所以我添加了 String[] cmd = {"bash", "-c",命令}; but it still doesn't work.但它仍然不起作用。

My command looks like this:我的命令如下所示:

/usr/src/calibre/ebook-convert /tmp/filesDirectory_mobi3970575619159760977/d7ed6792-b3cb-4761-bb6a-b9facf9e7a6c9250643820137116550.html /tmp/filesDirectory_mobi3970575619159760977/d7ed6792-b3cb-4761-bb6a-b9facf9e7a6c11909891397415910433.mobi /usr/src/calibre/ebook-convert /tmp/filesDirectory_mobi3970575619159760977/d7ed6792-b3cb-4761-bb6a-b9facf9e7a6c9250643820137116550.html /tmp/filesDirectory_mobi3970575619159760977/d7ed6792-b3cb-4761-bb6a-b9facf9e7a6c11909891397415910433.mobi

And here's my code:这是我的代码:

@Override
    public Document convert(Document document, DocumentFormat documentFormat) {
        Document htmlDocument = htmlDocumentConverter.convert(document, documentFormat);
        try {
            log.info("Converting document from {} to {}", getSourceFormat().toString(), getTargetFormat().toString());
            CalibreConfigData calibreData = calibreConfig.getConfigurationData(CalibreConversion.HTML_TO_MOBI);

            Files.write(calibreData.getSourceFilePath(), htmlDocument.getContent());

            String command = calibreData.getCalibreCommand();
            String[] cmd = {"bash", "-c", command};
            var r = Runtime.getRuntime().exec(cmd);
            r.waitFor();
            log.warn("Process: " + new String(r.getInputStream().readAllBytes(), StandardCharsets.UTF_8));

            byte[] convertedFileAsBytes = Files.readAllBytes(calibreData.getConvertedFilePath());

//            Files.deleteIfExists(calibreData.getSourceFilePath());
//            Files.deleteIfExists(calibreData.getConvertedFilePath());
//            Files.deleteIfExists(calibreData.getFilesDirectoryPath());

            return new Document(convertedFileAsBytes);
        } catch (InterruptedException | IOException e) {
            log.error("Conversion failed due to problem: " + e);
            throw new ConversionException("Conversion failed due to problem: " + e);
        }
    }

I checked created tempFiles and found out that file.html has content inside but file.mobi is empty even after executing above's command.我检查了创建的临时文件,发现 file.html 里面有内容,但是 file.mobi 即使在执行上面的命令后也是空的。

A main method of a Calibre conversion invoker could be as simple as the below: Calibre 转换调用程序的main方法可以像下面这样简单:

public static void main(String[] args) {
    try {
        String[] command = { "/usr/src/calibre/ebook-convert", args[0], args[1] };
        ProcessBuilder pb = new ProcessBuilder(command);
        pb.inheritIO();
        pb.start();
    } catch (Throwable t) {
        t.printStackTrace();
    }
}

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

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