Java ProcessBuilder示例大全

乔欣 Java经验 发布时间:2023-01-30 22:58:31 阅读数:8505 1
下文是笔者讲述ProcessBuilder示例大讲解,如下所示

ProcessBuilder类简介

ProcessBuilder命令的功能:
     使用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");
 
	Process process = processBuilder.start();

ProcessBuilder运行ping命令

package com.java265.process;
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下运行cmd
        processBuilder.command("cmd.exe", "/c", "ping -n 3 java265.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();
        }
    }
}

Ping +线程示例

 
package com.java265.process;
 
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.list;
import java.util.concurrent.*;
import java.util.stream.Collectors;
 
public class ProcessBuilderExample2 {
 
    public static void main(String[] args) {
         ExecutorService pool = Executors.newSingleThreadExecutor();
        ProcessBuilder processBuilder = new ProcessBuilder();
 
        // windows下运行cmd
        processBuilder.command("cmd.exe", "/c", "ping -n 3 java265.com");
 
        try {
 
            Process process = processBuilder.start();
 
            System.out.println("process ping...");
            ProcessReadTask task = new ProcessReadTask(process.getInputStream());
            Future<list<string>> future = pool.submit(task);
 
            //不阻塞
            System.out.println("process task1...");
            System.out.println("process task2...");
 
            List<string> result = future.get(5, TimeUnit.SECONDS);
            for (String s : result) {
                System.out.println(s);
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            pool.shutdown();
        }
    }
 
    private static class ProcessReadTask implements Callable<list<string>> {
 
        private InputStream inputStream;
 
        public ProcessReadTask(InputStream inputStream) {
            this.inputStream = inputStream;
        }
 
        @Override
        public List<string> call() {
            return new BufferedReader(new InputStreamReader(inputStream))
				.lines()
				.collect(Collectors.toList());
        }
    }
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202301/16750907605586.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者