java.io.IOException: 无法运行程序 "...": java.io.IOException: error=2, No such file or directory

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16482601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:07:38  来源:igfitidea点击:

java.io.IOException: Cannot run program "...": java.io.IOException: error=2, No such file or directory

java

提问by Azathoth

I need to convert a fodtfile to pdf. After days wasted trying to use a variety of converters I realized that using libreoffice could do the trick.

我需要将fodt文件转换为pdf。在尝试使用各种转换器浪费了几天之后,我意识到使用 libreoffice 可以解决问题。

/usr/bin/libreoffice --headless --convert-to pdf:'writer_pdf_Export' --outdir /home/develop/tomcat/mf/ROOT/private/docs/0/ /home/develop/tomcat/mf/ROOT/private/docs/0/35_invoice.fodt

But what works perfectly from the command line does not work in java...

但是从命令行完美运行的东西在java中不起作用......

java.io.IOException: Cannot run program "/usr/bin/libreoffice --headless --convert-to pdf:'writer_pdf_Export' --outdir /home/develop/tomcat/mf/ROOT/private/docs/0 /home/develop/tomcat/mf/ROOT/private/docs/0/35_invoice.fodt": java.io.IOException: error=2, No such file or directory

I followed many howtos and different approaches but the results are always the same.

我遵循了许多方法和不同的方法,但结果总是一样的。

The command:

命令

"/usr/bin/libreoffice --headless --convert-to pdf:'writer_pdf_Export' --outdir "+ getDestinationDirectory(order)+" "+getInvoiceFilename()+".fodt");

I tried all in one String and splitting into String[] too. In last case it complains about the parameter:

我在一个 String 中尝试了所有并拆分为 String[] 。在最后一种情况下,它抱怨参数:

Unknown option: --convert-to pdf:'writer_pdf_Export' --outdir /home/develop/tomcat/mf/ROOT/private/docs/0 /home/develop/tomcat/mf/ROOT/private/docs/0/35_invoice.fodt

Here is a sample of the last test

这是上次测试的示例

        List<String> command = new ArrayList<String>();
        command.add("/usr/bin/libreoffice");
        command.add("--headless");
        command.add("--convert-to pdf:'writer_pdf_Export' --outdir " + getDestinationDirectory(order) + " " + getInvoiceFilename() + ".fodt");

        ProcessBuilder builder = new ProcessBuilder(command);

        Process process = null;
        try {
            process = builder.start();
        } catch (IOException ex) {
            Logger.getLogger(Documents.class.getName()).log(Level.SEVERE, null, ex);
        }
        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;
        try {
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException ex) {
            Logger.getLogger(Documents.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Program terminated!");

采纳答案by xagyg

Try this (keep it simple) ...

试试这个(保持简单)...

Process p = Runtime.getRuntime().exec("/usr/bin/libreoffice --headless --convert-to pdf:'writer_pdf_Export' --outdir "+ getDestinationDirectory(order)+" "+getInvoiceFilename()+".fodt");

Fully ...

完全...

    Process process = null;
    try {
            process = Runtime.getRuntime().exec("/usr/bin/libreoffice --headless --convert-to pdf:'writer_pdf_Export' --outdir "+ getDestinationDirectory(order)+" "+getInvoiceFilename()+".fodt");
    } catch (IOException ex) {
        Logger.getLogger(Documents.class.getName()).log(Level.SEVERE, null, ex);
    }
    BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line;
    try {
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    } catch (IOException ex) {
        Logger.getLogger(Documents.class.getName()).log(Level.SEVERE, null, ex);
    }
    br.close();
    System.out.println("Program terminated!");

回答by Mike Samuel

Instead of

代替

command.add("--convert-to pdf:'writer_pdf_Export' --outdir " + getDestinationDirectory(order) + " " + getInvoiceFilename() + ".fodt")

try splitting each of the argv into its own call to add

尝试将每个 argv 拆分为自己的调用以添加

command.add("--convert-to");
command.add("pdf:writer_pdf_Export");

command.add("--outdir");
command.add(getDestinationDirectory(order).toString());

command.add(getInvoiceFilename() + ".fodt");

Note that there are no apostrophes around "writer_pdf_Export" since those are shell meta-characters and are not required when you're constructing an array to pass to execwithout an intermediating shell.

请注意,“writer_pdf_Export”周围没有撇号,因为它们是 shell 元字符,并且在您构造要传递给exec而没有中间 shell的数组时不需要这些撇号。

回答by er_benji

I have tried every solution proposed in this thread and it does not work.

我已经尝试了这个线程中提出的所有解决方案,但它不起作用。

In my app (java webapp using TOMCAT in linux) it only works to create a shell script and execute the script. But you have to put the absolute path in the script, if not, it does not work ($HOME does not work). Besides, you can pass it arguments.

在我的应用程序(在 linux 中使用 TOMCAT 的 java webapp)中,它只能创建一个 shell 脚本并执行该脚本。但是你必须在脚本中放置绝对路径,如果没有,它不起作用($HOME 不起作用)。此外,您可以向它传递参数。

Example:

例子:

Runtime.getRuntime().exec("/home/user/myscript.sh param1");