简体   繁体   English

无法运行程序“wkhtmltopdf”:错误=2,没有这样的文件或目录 - 从 Java 获取此错误

[英]Cannot run program "wkhtmltopdf": error=2, No such file or directory - Getting this error from Java

I am executing wkhtmltopdf command line tool from Java but it is throwing below error.我正在从 Java 执行 wkhtmltopdf 命令行工具,但它抛出错误。

Cannot run program "wkhtmltopdf": error=2, No such file or directory

But note that when I execute this command line tool from my mac terminal then pdf got generated successfully.但请注意,当我从我的 mac 终端执行此命令行工具时,pdf 已成功生成。 Please see below.请看下文。

MacBook-Air-2:~ inDiscover$ wkhtmltopdf /var/folders/7y/2vr28n113p908ksnk0fnpqch0000gn/T/test7896850081571855407.html /Users/mymac/Documents/Project/emailbody/test2.pdf
Loading pages (1/6)
Counting pages (2/6)                                               
Resolving links (4/6)                                                       
Loading headers and footers (5/6)                                           
Printing pages (6/6)
Done

I have seen lot many similar questions here (for ex: wkhtmltopdf: No such file or directory [ Closed ] ) but issue with those questions are related to $PATH .我在这里看到了很多类似的问题(例如: wkhtmltopdf: No such file or directory [ Closed ] ),但这些问题的问题与$PATH相关。 In my case I believe that I have set the path to the executable to $PATH correctly.就我而言,我相信我已将可执行文件的路径正确设置为$PATH Please see below.请看下文。

MacBook-Air-2:~ inDiscover$ locate wkhtmltopdf
/private/var/db/receipts/org.wkhtmltopdf.wkhtmltox.bom
/private/var/db/receipts/org.wkhtmltopdf.wkhtmltox.plist
/usr/local/bin/wkhtmltopdf
/usr/local/share/man/man1/wkhtmltopdf.1.gz

You can see here wkhtmltopdf has been added to $PATH (/usr/local/bin)您可以在这里看到wkhtmltopdf已添加到$PATH (/usr/local/bin)

Also, see below the response for echo $PATH .另外,请参阅下面的echo $PATH响应。

MacBook-Air-2:~ inDiscover$ echo $PATH
/Users/mymac/anaconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
MacBook-Air-2:~ inDiscover$ 

I am getting this issue only when I try to execute the command from Java.仅当我尝试从 Java 执行命令时才遇到此问题。 Please see my java code below.请在下面查看我的 java 代码。

String VIEWPORT_SIZE = "2480x3508";
        int CONVERSION_DPI = 300;
        int IMAGE_QUALITY = 100;
        
        List<String> cmd = new ArrayList<String>(Arrays.asList("wkhtmltopdf",
                "--viewport-size", VIEWPORT_SIZE,
                "--enable-local-file-access",
                // "--disable-smart-shrinking",
                "--dpi", String.valueOf(CONVERSION_DPI),
                "--image-quality", String.valueOf(IMAGE_QUALITY)));
        //cmd.addAll(extParams);
        cmd.add("/var/folders/7y/2vr28n113p908ksnk0fnpqch0000gn/T/test7896850081571855407.html");
        cmd.add("/Users/mymac/Documents/Project/emailbody/test3.pdf");
        
        
        try {
            ProcessBuilder pb = new ProcessBuilder(cmd);

            if (Logger.level.compareTo(LogLevel.Info) >= 0) {
                pb.inheritIO();
            }

            Process p = pb.start();
            p.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }

Am I really missing something?我真的错过了什么吗?

Here are a few things you can check, one will hopefully resolve your issue.您可以检查以下几点,希望能解决您的问题。

Nearly all occasions I've seen where java is not finding an application that your shell or terminal does run are down to use of commands built into the shell / terminal (such as an alias), or because of PATH used in a script that you use to launch your java yourapp.ClassName... is different to that set for the interactive shell / terminal. Nearly all occasions I've seen where java is not finding an application that your shell or terminal does run are down to use of commands built into the shell / terminal (such as an alias), or because of PATH used in a script that you用于启动您的java yourapp.ClassName...与交互式 shell / 终端的设置不同。

The interactive shell / terminal may include or source from other scripts on startup - for example it may have run code in ~/.bashrc , ~/.login , ~/.profile , ... meaning that PATH declared inside interactive shell is not same as PATH presented to Java app when you launched it.交互式 shell / 终端可能在启动时包含其他脚本或从其他脚本获取 - 例如,它可能在~/.bashrc~/.login~/.profile中运行了代码...这意味着在交互式 shell 中声明的 PATH 不是与启动时提供给 Java 应用程序的 PATH 相同。

Hence you see terminal show PATH has /usr/local/bin:因此,您会看到终端显示 PATH 有 /usr/local/bin:

MacBook-Air-2:~ inDiscover$ echo $PATH
/Users/mymac/anaconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

But Java says no /usr/local/bin in PATH:但是 Java 在 PATH 中说没有 /usr/local/bin:

System.out.println("PATH="+System.getenv("PATH"));
/usr/bin:/usr/sbin:/sbin

So to fix your problem you could make java use absolute path to run the app wkhtmltopdf:因此,要解决您的问题,您可以让 java 使用绝对路径来运行应用程序 wkhtmltopdf:

Arrays.asList("wkhtmltopdf" ->  Arrays.asList("/usr/local/bin/wkhtmltopdf" ...

OR you could make java launch your shell / terminal so the terminal sources its env scripts as normal and runs the command:或者您可以让 java 启动您的 shell / 终端,以便终端正常获取其 env 脚本并运行命令:

# Confirm which shell your Terminal uses:
echo $SHELL

If you have say SHELL=/bin/bash you can run /bin/bash from java and let it work out where wkhtmltopdf is:如果你说SHELL=/bin/bash你可以从 java 运行/bin/bash并让它找出 wkhtmltopdf 的位置:

Arrays.asList("wkhtmltopdf" ->  Arrays.asList("/bin/bash", "-c", "wkhtmltopdf" ...

OR if you have a script say runMyApp.sh to launch your java app, set the PATH before java yourclass.Name ,或者,如果您有一个脚本说runMyApp.sh来启动您的 java 应用程序,请在java yourclass.Name之前设置 PATH ,

export PATH=/usr/local/bin:$PATH
java yourclass.Name

OR if you have a script say runMyApp.sh to launch your java app, make that it sources the same profile environment as your Terminal does.或者,如果您有一个脚本说runMyApp.sh来启动您的 java 应用程序,请使其来源与您的终端相同的配置文件环境。 This depends on the SHELL but for some systems could be something like:这取决于 SHELL 但对于某些系统可能类似于:

#!/bin/bash

# Load env from current user - "source" and "." may or may not work in the SHELL you are using:
source ~/.bashrc
# OR maybe other shell
. ~/.somefilerc

echo $PATH # Now hopefully includes same /usr/local/bin
java yourclass.Name

Try to add sudo in the front of wkhtmltopdf.尝试在wkhtmltopdf前面加上sudo。

暂无
暂无

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

相关问题 无法运行程序“...”错误= 2,没有这样的文件或目录(java) - Cannot run program “…” error=2, No such file or directory (java) 无法运行程序“ osascript”:error = 2,没有这样的文件或目录 - Cannot run program “osascript”: error=2, No such file or directory 无法运行程序“ docker”,错误= 2,无此文件或目录 - Cannot run program “docker” error=2, No such file or directory 无法运行程序“mvn”错误=2,没有这样的文件或目录 - Cannot run program "mvn" error=2, No such file or directory im4java中出现错误,无法运行程序“转换”:错误= 2,没有此类文件或目录 - Error in im4java, Cannot run program “convert”: error=2, No such file or directory ant jar错误:执行失败:java.io.IOException:无法运行程序... $ {aapt}“:error = 2,没有这样的文件或目录 - ant jar error: Execute failed: java.io.IOException: Cannot run program…${aapt}": error=2, No such file or directory 无法启动测试系统&#39;slim&#39;:java.io.IOException:无法运行程序“ java”:error = 2,没有这样的文件或目录 - Unable to start test system 'slim': java.io.IOException: Cannot run program “java”: error=2, No such file or directory java.io.IOException: 无法运行程序“...”: java.io.IOException: error=2, No such file or directory - java.io.IOException: Cannot run program “…”: java.io.IOException: error=2, No such file or directory 我在OS X Mavericks上运行mvn时出现以下错误:无法运行程序“/ bin / sh”:error = 2,没有这样的文件或目录 - I am getting the following error when I run mvn on OS X Mavericks: Cannot run program “/bin/sh”: error=2, No such file or directory spark 2.0-java.io.IOException:无法运行程序“ jupyter”:error = 2,没有这样的文件或目录 - spark 2.0 - java.io.IOException: Cannot run program “jupyter”: error=2, No such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM