gpt4 book ai didi

java - 我们到底可以在 Process.exec() 的字符串数组中存储什么

转载 作者:行者123 更新时间:2023-11-30 06:46:06 28 4
gpt4 key购买 nike

我找不到任何地方可以具体解释 Process exec(String[] cmdarray)String[] cmdarray 中可以存储什么内容。方法。我找到了一些地方 cmdarray 来存储数组命令或文件位置和远程服务器名称。所以我想知道我们到底可以在 String[] cmdarray 中存储什么?

最佳答案

数组的第一个元素是命令,例如cmd。其他的都是论证。例如:

try {
Process p = Runtime.getRuntime().exec(new String[] {"cmd", "/c", "echo", "This", "is", "an", "argument"});
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s;
while((s = reader.readLine()) != null) {
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
}

这里“/c”“echo”“这个”"is""an""argument" 都是命令 cmd 的参数。输出为:

This is an argument 

如果要运行多个命令,则必须使用双“&”号来指示另一个命令正在启动:

try {
Process p = Runtime.getRuntime().exec(new String[] { "cmd", "/c", "echo", "This", "is", "an", "argument",
"&&", "echo", "this", "command", "snuck", "in" });
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s;
while ((s = reader.readLine()) != null) {
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
}

这里每个命令都被发送到 cmd。我并不肯定,但我相信你必须开始一个新的流程来向其他地方发送命令。输出为:

This is an argument 
this command snuck in

阅读此内容以获取更多信息:https://stackoverflow.com/a/18867097/5645656

关于java - 我们到底可以在 Process.exec() 的字符串数组中存储什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48011611/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com