public CGAOperationStatus downloadMetaData() {
CGAOperationStatus retCgaOperationStatus = new CGAOperationStatus();
try {
createDirectoryIfNeeded("/sure/replication/metadata");
String prepareRsyncCommand = "gsutil -m rsync -d -r gs://"
+ storeCredentials.getStoreAccount()
+ "/sure/metadata /sure/replication/metadata'";
Process p = Runtime.getRuntime().exec(prepareRsyncCommand);
p.waitFor();
} catch (Exception e) {
System.out.print("Exception in downloading metadata from GoogleBucket");
retCgaOperationStatus
.setScgaError(CGAError.SCGA_EXCEPTION_OCCURRED);
ExceptionHandler.logException(logger, e);
retCgaOperationStatus.setErrorMessage(e.getMessage());
}
return retCgaOperationStatus;
}
我已经在 Linux 机器上安装了 Gcloud SDK,我正在尝试从 Jar 文件 运行 gsutil 命令,我的 jar 是从一些外部程序通过 ssh 执行的。但它就是行不通。
如果我手动运行相同的命令,它工作正常。可能是因为我在运行命令时加载了 .bashrc。
我怎样才能从 java 做同样的事情。
最佳答案
您应该使用 String[] 而不是简单的 String 作为运行时调用中的参数。
Runtime.getRuntime().exec(new String[]{"gsutil", "-m", "rsync", "-d", "-r", "gs://"+ storeCredentials.getStoreAccount(), "/sure/metadata /sure/replication/metadata'"});
关于java - 如何在java程序中加载bashrc并执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37522934/