Java Runtime.getRuntime()。exec无法在命令中使用双引号

时间:2018-11-15 12:42:17

标签: java gcloud runtime.exec google-cloud-dataproc

以下命令在我的终端上可以正常工作:

gcloud日志读取为"logName=projects/logs/java.log AND labels.component=projet1 AND textPayload=\"End: of query.\"

但是在通过inputstream执行时返回空Runtime.getRuntime().exec

Runtime rt = Runtime.getRuntime();
Process process = rt.exec(new String[]{"gcloud", "logging" ,"read" ,"logName=projects/logs/java.log AND labels.component=project1 AND textPayload=\"End: of query.\""});
BufferedReader is = new BufferedReader(new 
InputStreamReader(process.getInputStream()));

更新: 我尝试使用processBuilder,命令参数如下所示:

命令是:[gcloud,日志记录,读取,'logName = projects / logs / java.log AND labels.component = project1 AND textPayload =“结尾:查询结束。” AND timestamp>“ 2018-11-15T15:12:11Z”']

但是执行时出现以下错误:

错误:(gcloud.logging.read)INVALID_ARGUMENT:无法解析的过滤器:第1行第147列的语法错误,令牌为““ 2018-11-15T15:12:11Z”“

1 个答案:

答案 0 :(得分:3)

您需要两次\转义,这在Java中很常见,当我们调用另一个需要转义的东西(正则表达式或其他)

尝试一下(我只将\“替换为\\”)

Runtime rt = Runtime.getRuntime();
Process process = rt.exec(new String[]{"gcloud", "logging" ,"read"  
,"logName=projects/logs/java.log AND labels.component=project1 AND textPayload=\\\"End: of query.\\\""});
BufferedReader is = new BufferedReader(new 
InputStreamReader(process.getInputStream()));
  • \:转至\

  • \”:转至“

  • 因此\\“将在runtime.exec中转义为\”