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

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. 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 a a file and create another file. This works when run manually but not from within a java app making the same shell call and with no errors.

Just to be sure I even tried chmod 777 on the container folder so I don't think its a permission issue.

Shell Executor Code (Courtesy of 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

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:

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"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值