Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 命令从exec()失败,但在终端上有效_Java_Linux_Runtime.exec_Pdftotext - Fatal编程技术网

Java 命令从exec()失败,但在终端上有效

Java 命令从exec()失败,但在终端上有效,java,linux,runtime.exec,pdftotext,Java,Linux,Runtime.exec,Pdftotext,我正在尝试使用Java将pdf转换为txt。我尝试过ApachePDFBox,但由于一些奇怪的原因,它无法转换整个文档。因此,我决定通过执行Runtime.getRuntime().exec()调用来使用pdftotext。问题是,虽然在我的终端pdftotext上工作正常,但exec()调用会给我错误代码1(有时甚至是99)。 这是电话: pdftotext "/home/www-data/CANEFS_TEST/Hello/ciao.pdf" "/tmp/ciao.pdf.txt" 这是密

我正在尝试使用Java将pdf转换为txt。我尝试过ApachePDFBox,但由于一些奇怪的原因,它无法转换整个文档。因此,我决定通过执行Runtime.getRuntime().exec()调用来使用pdftotext。问题是,虽然在我的终端pdftotext上工作正常,但exec()调用会给我错误代码1(有时甚至是99)。 这是电话:

pdftotext "/home/www-data/CANEFS_TEST/Hello/ciao.pdf" "/tmp/ciao.pdf.txt"
这是密码

private static File callPDF2Text(File input,File output){
    assert input.exists();
    assert Utils.getExtension(input).equalsIgnoreCase("pdf");
    assert Utils.getExtension(output).equalsIgnoreCase("txt") : output.getAbsoluteFile().toString();

    Process p=null;

    try {
        System.out.println(String.format(
                PDF2TXT_COMMAND,
                input.getAbsolutePath(),
                output.getAbsolutePath()));
        p=Runtime.getRuntime().exec(String.format(
                PDF2TXT_COMMAND,
                input.getAbsolutePath(),
                output.getAbsolutePath()));
        p.waitFor();
        if (p.exitValue()!=0){
            throw new RuntimeException("exit value for pdftotext is "+p.exitValue());
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return output;
}
以下是PDF2TXT_命令字符串定义:

public static final String PDFTXT_COMMAND="pdftotext \"%s\" \"%s\"";
我知道这类错误通常是由权限设置引起的。下面是Hello文件夹中ls-l命令的输出:

ls -l /home/www-data/CANEFS_TEST/Hello/
total 136
-rwxrwxr-- 1 www-data www-data 136041 mar 27 16:31 ciao.pdf
另外,请注意,创建流程的用户是koldar,它位于www数据组中。 谢谢你的时间和耐心

不要在格式字符串中使用“…这些字符由shell专门解析,您不需要使用shell来启动命令

我可以建议您使用
exec(String[])
而不是
exec(String)
,这样您就可以分离命令的每个参数:

String []command = new String[3];
command[0] = "pdftotext";
command[1] = input.getAbsolutePath();
command[2] = output.getAbsolutePath();
Runtime.getRuntime().exec(command);
这应该行得通。如果不行,那可能是目录的访问权限问题。

不要在格式字符串中使用“No…”。。。这些字符由shell专门解析,您不需要使用shell来启动命令

我可以建议您使用
exec(String[])
而不是
exec(String)
,这样您就可以分离命令的每个参数:

String []command = new String[3];
command[0] = "pdftotext";
command[1] = input.getAbsolutePath();
command[2] = output.getAbsolutePath();
Runtime.getRuntime().exec(command);

这应该行得通。如果没有,这可能是目录的访问权限问题。

好的,这是可行的,但是如果pdf文件名中有空格,会发生什么情况?不要介意。。。它也有用!非常感谢你!这是保证工作!每个参数将按原样传递给系统!您必须在shell中使用“因为您想说:这是一个单独的参数。使用
exec(字符串[])
绕过任何解析。这确实是一个非常愚蠢的问题。再次感谢!好的,这是可行的,但如果pdf文件名中有空格会发生什么情况?没关系……它也可以!非常感谢!这保证会起作用!每个参数都会按原样传递到系统!您的用户必须使用“使用shell,因为您想说:这是单个arg。使用
exec(String[])
绕过任何解析。这确实是一个非常愚蠢的问题。再次感谢!