[英]How to execute multiple commands in a single terminal using Java?
try
{
Runtime rt = Runtime.getRuntime();
rt.exec("cmd /c start cmd.exe /K \"java -version\"");
System.out.println("After completing the first command");
rt.exec("cmd /c start cmd.exe /K \"javac -version\"");
}
catch (Exception e)
{
System.out.println("Something wrong");
e.printStackTrace();
}
By using the above program I am able to execute commands in a terminal, but it opens multiple instances of the terminal.通过使用上述程序,我可以在终端中执行命令,但它会打开终端的多个实例。 I want to execute both commands in the same terminal.我想在同一个终端中执行这两个命令。 Is this possible?这可能吗?
As suggested, you can put your commands in a .bat
file that you will run from java program or you can use the &
or the &&
operator. 根据建议,您可以将命令放在将从Java程序运行的.bat
文件中,也可以使用&
或&&
运算符。
(The difference between the two operators is that &&
will let the second command be executed only if the first succeeded.) (两个运算符之间的区别是&&
仅在第一个命令成功的情况下才让第二个命令执行。)
Try this: 尝试这个:
rt.exec("cmd /c start cmd.exe /K \"java -version && javac -version\"");
You could also use a ProcessBuilder java API to do this. 您也可以使用ProcessBuilder Java API来执行此操作。 By default Runtime.getRuntime().exec will tokenize the input, in the case of ProcessBuilder it will execute the command as it is. 默认情况下,Runtime.getRuntime()。exec将标记输入,对于ProcessBuilder,它将按原样执行命令。
public class TestCommands {
public static void main(String[] args) throws IOException, InterruptedException {
String[] commands = {"echo hello","echo hi","java -version"};
for(String command : commands) {
execute(command);
}
}
public static void execute(String command) throws InterruptedException, IOException {
ProcessBuilder builder = new ProcessBuilder("cmd.exe","/c",command);
Process process = builder.inheritIO().start();
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String readline;
while ((readline = reader.readLine()) != null) {
System.out.println(readline);
}
}
}
If anyone is looking for a solution for macOS, I made one:如果有人正在寻找 macOS 的解决方案,我做了一个:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CustomDesktop {
public static void main(String[] args) throws IOException, InterruptedException {
// Execute multiple commands in one terminal.
final String[] TERMINAL_COMMANDS = {"cd ~/Videos", "ls -l"};
StringBuilder command = new StringBuilder(); // Using StringBuilder is recommended: less memory and faster.
for (String terminalCommand : TERMINAL_COMMANDS) {
command.append(terminalCommand).append(';');
}
execute(command.toString());
// Direct method call, if you prefer.
execute("cd ~/Videos; ls -l");
}
public static void execute(String command) throws IOException, InterruptedException {
// Use "bash" instead if your Mac runs pre-Catalina.
ProcessBuilder builder = new ProcessBuilder("zsh", "-c", command);
Process process = builder.inheritIO().start();
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String readline;
while ((readline = reader.readLine()) != null) {
System.out.println(readline);
}
}
}
Output: Output:
total 539296
-rw-r--r--@ 1 macintoshfan staff 276116282 Jun 18 14:14 Java.mov
total 539296
-rw-r--r--@ 1 macintoshfan staff 276116282 Jun 18 14:14 Java.mov
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.