如何在GCP上的Java代码中执行Shell命令

huangapple 未分类评论47阅读模式
英文:

How to execute shell command from Java code on GCP

问题

我的目标非常简单:
我想从位于Google Cloud Platform上(在我这个案例中是App Engine)的Java代码中执行一个shell命令(例如"pwd")。
在本地,我可以通过ProcessBuilder或者Runtime.getRuntime().exec(...)来实现。
问题是,当我尝试在需要执行这种操作的GCP上部署Java服务时,它失败并显示错误:
java.io.IOException: 无法运行程序"pwd":error=2,没有那个文件或目录

我尝试了以下方法:
Runtime.getRuntime().exec(new String[]{"bash", "-l", "-c", "pwd"}, null);
这种方法在本地也能工作,但在GCP上仍然失败。
有人知道怎么解决吗?

英文:

My goal is pretty simple:
I wan't to execute a shell command (e.g. "pwd") from java code that is on Google Cloud Platform (App Engine in my case).
Locally i'm capable of doing so either by a ProcessBuilder or Runtime.getRuntime().exec(...).
The thing is that when I'm trying to deploy a Java service on GCP that needs to do that, it fails with the error:
java.io.IOException: Cannot run program "pwd": error=2, No such file or directory

I tried working this out with:
Runtime.getRuntime().exec(new String[]{"bash", "-l", "-c", "pwd"}, null);
Which also works locally, but on GCP it still fails.
Anyone?

huangapple
  • 本文由 发表于 2020年5月4日 06:21:58
  • 转载请务必保留本文链接:https://java.coder-hub.com/61582419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定