java如何运行shell命令呢?
下文笔者讲述使用java代码运行shell命令的方法分享,如下所示
java代码运行shell命令
使用ProcessBuilder或Runtime.getRuntime().exec 即可运行shell命令
ProcessBuilder
ProcessBuilder processBuilder = new ProcessBuilder(); // -- Linux -- // Run a shell command processBuilder.command("bash", "-c", "ls /home/java265/"); // Run a shell script //processBuilder.command("path/to/hello.sh"); // -- Windows -- // Run a command //processBuilder.command("cmd.exe", "/c", "dir C:\\Users\\java265"); // Run a bat file //processBuilder.command("C:\\Users\\java265\\hello.bat"); try { Process process = processBuilder.start(); StringBuilder output = new StringBuilder(); BufferedReader reader = new BufferedReader( new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { output.append(line + "\n"); } int exitVal = process.waitFor(); if (exitVal == 0) { System.out.println("Success!"); System.out.println(output); System.exit(0); } else { //abnormal... } } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); }
Runtime.getRuntime().exec()
try { // -- Linux -- // Run a shell command // Process process = Runtime.getRuntime().exec("ls /home/java265/"); // Run a shell script // Process process = Runtime.getRuntime().exec("path/to/hello.sh"); // -- Windows -- // Run a command //Process process = Runtime.getRuntime().exec("cmd /c dir C:\\Users\\java265"); //Run a bat file Process process = Runtime.getRuntime().exec( "cmd /c hello.bat", null, new File("C:\\Users\\java265\\")); StringBuilder output = new StringBuilder(); BufferedReader reader = new BufferedReader( new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { output.append(line + "\n"); } int exitVal = process.waitFor(); if (exitVal == 0) { System.out.println("Success!"); System.out.println(output); System.exit(0); } else { //abnormal... } } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }
PING
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class ProcessBuilderExample1 { public static void main(String[] args) { ProcessBuilder processBuilder = new ProcessBuilder(); // Windows processBuilder.command("cmd.exe", "/c", "ping -n 3 google.com"); try { Process process = processBuilder.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } int exitCode = process.waitFor(); System.out.println("\nExited with error code : " + exitCode); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
host获取主机名
host -ta java265.com
package com.java265.shell; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arraylist; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ExecuteShellComand { private static final String IPADDRESS_PATTERN = "([01]?\\d\\d?|2[0-4]\\d|25[0-5])" + "\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])" + "\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])" + "\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])"; private static Pattern pattern = Pattern.compile(IPADDRESS_PATTERN); private static Matcher matcher; public static void main(String[] args) { ExecuteShellComand obj = new ExecuteShellComand(); String domainName = "java265.com"; String command = "host -t a " + domainName; String output = obj.executeCommand(command); //System.out.println(output); List<string> list = obj.getIpAddress(output); if (list.size() > 0) { System.out.printf("%s has address : %n", domainName); for (String ip : list) { System.out.println(ip); } } else { System.out.printf("%s has NO address. %n", domainName); } } private String executeCommand(String command) { StringBuffer output = new StringBuffer(); Process p; try { p = Runtime.getRuntime().exec(command); p.waitFor(); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = ""; while ((line = reader.readLine())!= null) { output.append(line + "\n"); } } catch (Exception e) { e.printStackTrace(); } return output.toString(); } public List<string> getIpAddress(String msg) { List<string> ipList = new ArrayList<string>(); if (msg == null || msg.equals("")) return ipList; matcher = pattern.matcher(msg); while (matcher.find()) { ipList.add(matcher.group(0)); } return ipList; } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。