[英]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.