【发布时间】:2021-09-18 22:35:31
【问题描述】:
从 Groovy/Java 我尝试执行以下命令:cmd /c echo mytext。
import java.nio.charset.Charset
println(Charset.defaultCharset().displayName()) //returns windows-1250
//in console chcp returns 852
def arg = "/c echo mytext"
def pb = new ProcessBuilder("cmd", arg)
def proc = pb.start()
def stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream(), "CP852"))
def stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream(), "CP852"))
def line = null
println("Here is the standard output of: cmd " + arg)
while ((line = stdInput.readLine()) != null) {
println(line)
}
println("Here is the standard error of the command (if any):")
while ((line = stdError.readLine()) != null) {
println(line)
}
Groovy:2.4.21、3.0.9
Java:zulu11.50.19-ca-fx-jdk11.0.12-win_x64
结果是mytext"(包括结束双引号)。我无法弄清楚为什么会有双引号。谁能帮我解释一下为什么会在那里?
谢谢。
【问题讨论】:
-
ProcessBuilder("cmd", "/c", "echo", "mytext")应该有帮助。 -
@daggett 谢谢,它有效。如此简单,它并没有发生在我身上。而且我仍然对造成双重问题的原因感到好奇。你知道它为什么在那里吗?
-
“我仍然对造成双引号的原因感到好奇。你知道它为什么会出现吗?” - 在代码
ProcessBuilder("cmd", "/c", "echo", "mytext")中,您可以将双引号替换为单引号并具有完全相同的行为。
标签: java groovy process external