[英]Issue while running Linux Command using Java?
I am running command using Java and getting no output. 我正在使用Java运行命令,但没有输出。
Process p;
Runtime run = Runtime.getRuntime();
String s1 = "queryData 1005017 --format '\"%s" scope'";
System.out.println("Command is " + s1);
try {
p = run.exec(s1);
BufferedReader br = new BufferedReader(
new InputStreamReader(p.getInputStream()));
while ((s = br.readLine()) != null)
System.out.println("line: " + s);
p.getErrorStream();
p.waitFor();
}
While the same command ---> queryData 1005017 --format '"%s" scope' runs without any issue. 虽然相同的命令---> queryData 1005017 --format'“%s” scope'可以正常运行。 Wondering am i missing any thing while handling either double quote, or % sign?
想知道我在处理双引号或%号时是否缺少任何东西吗?
You didn't escape the internal quotes properly: 您没有正确地转义内部引号:
String s1 = "queryData 1005017 --format '\"%s" scope'";
^--start java string ^--end java string
^^^^^ what's this mean to java?
You probably want 你可能想要
String s1 = "queryData 1005017 --format '\"%s\" scope'";
^--note this
instead. 代替。
Try do NOT use strings to start processes from Java. 尝试不要使用字符串从Java启动进程。 Correct way is usage of ProcessBuilder :
正确的方法是使用ProcessBuilder :
p = new ProcessBuilder(Arrays.asList(
"queryData", "1005017", "--format", "\"%s\" scope")).start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.