英文:
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?
专注分享java语言的经验与见解,让所有开发者获益!
评论