Java运行shell脚本方法
Java可以通过ProcessBuilder,Runtime和Process三种方式运行操作系统的命令,包括执行shell脚本。下面将详细讲解如何使用Java运行shell脚本。
方法一:ProcessBuilder
ProcessBuilder可以创建一个进程来执行操作系统命令。可以通过设置ProcessBuilder的参数来执行shell脚本。
示例一:执行简单的shell脚本,如显示当前目录下的文件列表。
ProcessBuilder pb = new ProcessBuilder("ls", "-l");
pb.directory(new File("."));
Process process = pb.start();
InputStream inputStream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
process.waitFor();
示例二:执行包含变量的shell脚本,如执行带参数的脚本。
String[] cmdarray = {"/bin/sh", "-c", "echo $1;echo $2"};
ProcessBuilder pb = new ProcessBuilder(cmdarray);
Map<String, String> env = pb.environment();
env.put("VAR1", "Hello");
env.put("VAR2", "World");
Process process = pb.start();
OutputStream outputStream = process.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream));
writer.println("$VAR1");
writer.flush();
writer.println("$VAR2");
writer.flush();
writer.close();
InputStream inputStream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
process.waitFor();
方法二:Runtime
使用Runtime也可以运行shell脚本。
示例三:执行简单的shell脚本,如显示当前目录下的文件列表。
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("ls -l");
InputStream inputStream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
process.waitFor();
示例四:执行带参数的shell脚本,如执行带变量的脚本。
Process process = runtime.exec(new String[] { "/bin/sh", "-c", "echo $VAR1;echo $VAR2" });
OutputStream outputStream = process.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream));
writer.println("VAR1=Hello");
writer.flush();
writer.println("VAR2=World");
writer.flush();
writer.close();
InputStream inputStream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
process.waitFor();
方法三:Process
使用Process也可以运行shell脚本。
示例五:执行简单的shell脚本,如显示当前目录下的文件列表。
Process process = ProcessBuilder.start(new String[] { "/bin/sh", "-c", "ls -l" });
InputStream inputStream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
process.waitFor();
示例六:执行带参数的shell脚本,如执行带变量的脚本。
Process process = ProcessBuilder.start(new String[] { "/bin/sh", "-c", "echo $VAR3" });
OutputStream outputStream = process.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream));
writer.println("VAR3=Hello World");
writer.flush();
writer.close();
InputStream inputStream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
process.waitFor();
以上就是Java运行shell脚本的完整攻略,包括ProcessBuilder、Runtime和Process三种方式的示例代码。您可以根据实际需求选择合适的方式。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:java运行shell脚本方法示例 - Python技术站