如何在调用(当前)shell 和 Java 中 运行 shell 命令
How to run a shell command in the calling (current) shell with Java
假设是这样的:
execInCurrentShell("cd /")
System.out.println("Ran command : cd /")
在MyClass
的main()
函数中
所以当我运行 class时,我cd
进入/
目录
user@comp [~] pwd
/Users/user
user@comp [~] java MyClass
Ran command : cd /
user@comp [/] pwd
/
常用运行shell命令的方式,即通过Runtime
class:
Runtime.getRuntime().exec("cd /")
不会在这里工作,因为它不是 运行 当前 shell 中的命令,而是新的 shell.
中的命令
execInCurrentShell()
函数(实际有效的函数)会是什么样子?
在Windows上从Java程序启动命令shell,您可以按如下方式进行:
import java.io.IOException;
public class Command {
public static void main(String[] args) {
try {
Runtime.getRuntime().exec("cmd.exe /c start");
} catch (IOException e) {
e.printStackTrace();
}
}
}
您需要对 Linux 使用相同的方法。
您将无法 运行 影响当前调用 shell 的命令,只能 运行 命令行 bash/cmd 作为来自 Java 并按如下方式向他们发送命令。我不推荐这种方法:
String[] cmd = new String[] { "/bin/bash" }; // "CMD.EXE"
ProcessBuilder pb = new ProcessBuilder(cmd);
Path out = Path.of(cmd[0]+"-stdout.log");
Path err = Path.of(cmd[0]+"-stderr.log");
pb.redirectOutput(out.toFile());
pb.redirectError(err.toFile());
Process p = pb.start();
String lineSep = System.lineSeparator();
try(PrintStream stdin = new PrintStream(p.getOutputStream(), true))
{
stdin.print("pwd");
stdin.print(lineSep);
stdin.print("cd ..");
stdin.print(lineSep);
stdin.print("pwd");
stdin.print(lineSep);
};
p.waitFor();
System.out.println("OUTPUT:"+Files.readString(out));
System.out.println("ERROR WAS: "+Files.readString(err));
}
这也适用于 Windows 上的 CMD.EXE(使用不同的命令)。要捕获每个命令的响应,如果您确实需要每行而不是一个文件的响应,您应该将 pb.redirectOutput()
的使用替换为读取 pb.getInputStream() 的代码。
假设是这样的:
execInCurrentShell("cd /")
System.out.println("Ran command : cd /")
在MyClass
main()
函数中
所以当我运行 class时,我cd
进入/
目录
user@comp [~] pwd
/Users/user
user@comp [~] java MyClass
Ran command : cd /
user@comp [/] pwd
/
常用运行shell命令的方式,即通过Runtime
class:
Runtime.getRuntime().exec("cd /")
不会在这里工作,因为它不是 运行 当前 shell 中的命令,而是新的 shell.
中的命令execInCurrentShell()
函数(实际有效的函数)会是什么样子?
在Windows上从Java程序启动命令shell,您可以按如下方式进行:
import java.io.IOException;
public class Command {
public static void main(String[] args) {
try {
Runtime.getRuntime().exec("cmd.exe /c start");
} catch (IOException e) {
e.printStackTrace();
}
}
}
您需要对 Linux 使用相同的方法。
您将无法 运行 影响当前调用 shell 的命令,只能 运行 命令行 bash/cmd 作为来自 Java 并按如下方式向他们发送命令。我不推荐这种方法:
String[] cmd = new String[] { "/bin/bash" }; // "CMD.EXE"
ProcessBuilder pb = new ProcessBuilder(cmd);
Path out = Path.of(cmd[0]+"-stdout.log");
Path err = Path.of(cmd[0]+"-stderr.log");
pb.redirectOutput(out.toFile());
pb.redirectError(err.toFile());
Process p = pb.start();
String lineSep = System.lineSeparator();
try(PrintStream stdin = new PrintStream(p.getOutputStream(), true))
{
stdin.print("pwd");
stdin.print(lineSep);
stdin.print("cd ..");
stdin.print(lineSep);
stdin.print("pwd");
stdin.print(lineSep);
};
p.waitFor();
System.out.println("OUTPUT:"+Files.readString(out));
System.out.println("ERROR WAS: "+Files.readString(err));
}
这也适用于 Windows 上的 CMD.EXE(使用不同的命令)。要捕获每个命令的响应,如果您确实需要每行而不是一个文件的响应,您应该将 pb.redirectOutput()
的使用替换为读取 pb.getInputStream() 的代码。