简体   繁体   English

尝试从Java运行gsutils永远不会返回

[英]trying to run gsutils from java never returns

I try to download folders from Google storage on the cloud. 我尝试从云端的Google存储空间下载文件夹。

I run from a process of a user that have permissions (when i run from regular terminal on mac it works) 我从具有权限的用户进程运行(当我从Mac上的常规终端运行时可以运行)

I have this code: 我有以下代码:

public void runCommand() {
    final Process p;
    try {
        p = Runtime.getRuntime().exec(
            "gsutil -m cp -r gs://my_bucket/705/201609040613/output/html_pages file:/Users/eladb/WorkspaceQa/GsClient/build/resources/main/downloads/");

        new Thread(new Runnable() {
        public void run() {
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;

            try {
                while ((line = input.readLine()) != null)
                    System.out.println(line);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();

    p.waitFor();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

but the new thread never returns. 但是新线程永远不会返回。

stuck on line: 卡在网上:

while ((line = input.readLine()) != null)

Is there any way to download these folders from google cloud otherwise? 否则可以从Google Cloud下载这些文件夹吗?

This manual copying of stdout data is error prone (you would have to forcefully close the stream to terminate the sub thread) and thankfully, unnecessary since Java 7 : 手动复制stdout数据容易出错(您将不得不强制关闭流以终止子线程),而且值得庆幸的是, 自Java 7以来 ,这是不必要的

public void runCommand() {
    try {
        new ProcessBuilder("gsutil", "-m", "cp", "-r",
            "gs://my_bucket/705/201609040613/output/html_pages",
            "file:/Users/eladb/WorkspaceQa/GsClient/build/resources/main/downloads/")
        .inheritIO()
        .start()
        .waitFor();
    } catch(IOException | InterruptedException e) {
        e.printStackTrace();
    }
}

If you don't want to direct all three channels this way, see redirectOutput(File) , redirectOutput(ProcessBuilder.Redirect) , and the similar methods for input and error channel. 如果您不想以这种方式redirectOutput(File)所有三个通道,请参见redirectOutput(File)redirectOutput(ProcessBuilder.Redirect)以及用于输入和错误通道的类似方法。


Only the (default) mode ProcessBuilder.Redirect.PIPE requires you to provide input or receive output while the sub-process is runnig. 仅(默认)模式ProcessBuilder.Redirect.PIPE要求您在运行子流程时提供输入或接收输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM