简体   繁体   English

Shell 命令从 Java 失败,但在手动运行时有效

[英]Shell command fails from java but works when run manually

I'm trying to run some bash commands that works fine from my console but fails when trying to make the same command call from within Java.我正在尝试运行一些 bash 命令,这些命令在我的控制台中运行良好,但在尝试从 Java 中进行相同的命令调用时失败。 The command returns no errors and fails to produce the desired output file.该命令不会返回任何错误并且无法生成所需的输出文件。 The command is suppose to use a PGP tool (GPG) to decrypt aa file and create another file.该命令假设使用 PGP 工具 (GPG) 解密一个文件并创建另一个文件。 This works when run manually but not from within a java app making the same shell call and with no errors.这在手动运行时有效,但不能在执行相同 shell 调用且没有错误的 Java 应用程序中运行。

Just to be sure I even tried chmod 777 on the container folder so I don't think its a permission issue.为了确保我什至在容器文件夹上尝试了 chmod 777,所以我认为这不是权限问题。

Shell Executor Code (Courtesy of Mkyong.com) Shell Executor Code(由 Mkyong.com 提供)

private static String executeCommand(String command) {

    StringBuffer output = new StringBuffer();

    Process p;
    try {
        p = Runtime.getRuntime().exec(command);
        p.waitFor();
        BufferedReader reader =
                new BufferedReader(new InputStreamReader(p.getInputStream()));

        String line = "";
        while ((line = reader.readLine())!= null) {
            output.append(line + "\n");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return output.toString();

}

Actual Shell Command实际的 Shell 命令

gpg --output /main/decrypted-token.txt --passphrase test /main/token.asc

If the command works fine on the terminal but not when calling from the Java script, the first thing I would try would be to specify Bash on the command being called, see if this works:如果该命令在终端上运行良好,但在从 Java 脚本调用时不起作用,我会尝试的第一件事是在被调用的命令上指定 Bash,看看这是否有效:

bash -c "gpg --output /main/decrypted-token.txt --passphrase test /main/token.asc"

Or even better:或者甚至更好:

/bin/bash -c "gpg --output /main/decrypted-token.txt --passphrase test /main/token.asc"

暂无
暂无

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

相关问题 Shell 文件在手动运行时有效,但在 CRON 中运行时失败 - Shell file works when run manually but fails when run in CRON 从java运行shell命令 - run shell command from java Javac 命令在手动输入时有效,但在 Makefile 中运行时无效 - Javac command works when typed manually but not when run in Makefile 从Java / Groovy运行复合Shell命令 - Run a compound shell command from Java/Groovy 无法从Java在Shell上运行apktool命令 - Unable to run apktool command on shell from Java mysqldump从java运行时返回代码6,但同样的命令从命令行运行正常 - mysqldump returns code 6 when run from java, but the same command works fine from command line 通过Runtime.exec()执行时命令失败,但在Raspberry Pi(Linux)上手动执行时命令起作用 - Command fails when executed through Runtime.exec() but works when executed manually at Raspberry Pi (Linux) 命令Java在交互式Shell中有效,但在Shell脚本中无效 - Command java works in interactive shell but not in shell script 使用通配符时,Javac找不到符号,但是当手动指定.java文件时,Javac起作用 - Javac fails to find symbol when wildcard is used, but works when .java file is manually specified 从Java触发时,DTEXEC不会在Power Shell中触发dtsx包,但可以在本地计算机上的命令提示符下工作 - DTEXEC doesn't trigger dtsx package in a power shell when triggered from java but works from command prompt on local machine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM