Java ProcessBuilder示例 布满荆棘的人生 2023-02-15 05:10 21阅读 0赞 在Java中,我们可以使用`ProcessBuilder`轻松调用外部命令: ProcessBuilder processBuilder = new ProcessBuilder(); // -- Linux -- // Run a shell command processBuilder.command("bash", "-c", "ls /home/mkyong/"); // Run a shell script processBuilder.command("path/to/hello.sh"); // -- Windows -- // Run a command processBuilder.command("cmd.exe", "/c", "dir C:\\Users\\mkyong"); // Run a bat file processBuilder.command("C:\\Users\\mkyong\\hello.bat"); Process process = processBuilder.start(); ## 1.平 ## 1.1运行外部ping命令对网站进行ping操作3次,并显示输出信息。 ProcessBuilderExample1.java package com.mkyong.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(); // Run this on Windows, cmd, /c = terminate after this run processBuilder.command("cmd.exe", "/c", "ping -n 3 google.com"); try { Process process = processBuilder.start(); // blocked :( 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(); } } } 输出量 Pinging google.com [172.217.166.142] with 32 bytes of data: Reply from 172.217.166.142: bytes=32 time=10ms TTL=55 Reply from 172.217.166.142: bytes=32 time=10ms TTL=55 Reply from 172.217.166.142: bytes=32 time=10ms TTL=55 Ping statistics for 172.217.166.142: Packets: Sent = 3, Received = 3, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 10ms, Maximum = 10ms, Average = 10ms Exited with error code : 0 ## 2. Ping +线程 ## 在上面的示例1.1中, `process.getInputStream`是“ blocking”,最好为读取过程启动一个新的Thread,这样它就不会阻止其他任务。 ProcessBuilderExample2.java package com.mkyong.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(); // Run this on Windows, cmd, /c = terminate after this run processBuilder.command("cmd.exe", "/c", "ping -n 3 google.com"); try { Process process = processBuilder.start(); System.out.println("process ping..."); ProcessReadTask task = new ProcessReadTask(process.getInputStream()); Future<list<string>> future = pool.submit(task); // no block, can do other tasks here 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()); } } } 输出量 process ping... process task1... process task2... Pinging google.com [172.217.166.142] with 32 bytes of data: Reply from 172.217.166.142: bytes=32 time=11ms TTL=55 Reply from 172.217.166.142: bytes=32 time=10ms TTL=55 Reply from 172.217.166.142: bytes=32 time=10ms TTL=55 Ping statistics for 172.217.166.142: Packets: Sent = 3, Received = 3, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 10ms, Maximum = 11ms, Average = 10ms ## 3.更改目录 ## 3.1转到目录`C:\\users`并运行external `dir`命令列出所有文件。 ProcessBuilderExample3.java package com.mkyong.process; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; public class ProcessBuilderExample3 { public static void main(String[] args) { ProcessBuilder processBuilder = new ProcessBuilder(); processBuilder.command("cmd.exe", "/c", "dir"); processBuilder.directory(new File("C:\\users")); // can also run the java file like this // processBuilder.command("java", "Hello"); 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(); } } } 输出量 Volume in drive C has no label. Volume Serial Number is CE5B-B4C5 Directory of C:\users //... ## 参考文献 ## 1. [Microsoft系统错误代码(0-499)][Microsoft_0-499] 2. [如何从Java执行Shell命令][Java_Shell] 3. [Java文档– ProcessBuilder][Java_ ProcessBuilder] 4. [cmd / C是什么意思?][cmd _ C] 标签: [可调用的][Link 1] [外部命令][Link 2] [java][] [进程][Link 3] [shell命令][shell] [线程][Link 4] > 翻译自: [https://mkyong.com/java/java-processbuilder-examples/][https_mkyong.com_java_java-processbuilder-examples] [Microsoft_0-499]: https://docs.microsoft.com/en-us/windows/desktop/debug/system-error-codes--0-499- [Java_Shell]: https://www.mkyong.com/java/how-to-execute-shell-command-from-java/ [Java_ ProcessBuilder]: https://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html [cmd _ C]: https://stackoverflow.com/questions/515309/what-does-cmd-c-mean [Link 1]: https://mkyong.com/tag/callable/ [Link 2]: https://mkyong.com/tag/external-command/ [java]: https://mkyong.com/tag/java/ [Link 3]: https://mkyong.com/tag/process/ [shell]: https://mkyong.com/tag/shell-command/ [Link 4]: https://mkyong.com/tag/thread/ [https_mkyong.com_java_java-processbuilder-examples]: https://mkyong.com/java/java-processbuilder-examples/
相关 Java进程ProcessBuilder类的介绍及使用,ProcessBuilder调用外部程序执行shell命令Linux命令 目录 ProcessBuilder类的介绍及使用 【前言】 【正文】 \--构造方法-- \--常用方法-- \--使用技巧-- \--调用本地Shell命令,实 短命女/ 2024年03月16日 19:02/ 0 赞/ 31 阅读
相关 使用 Spring Boot 中的 Java ProcessBuilder 的 waitFor() 方法 1.前言 在Spring Boot应用程序中执行外部进程是一项常见任务。ProcessBuilder类提供了一种简单且灵活的方式来创建和启动外部进程,并且waitFor( 「爱情、让人受尽委屈。」/ 2024年02月05日 13:59/ 0 赞/ 32 阅读
相关 使用 Spring Boot 中的 Java ProcessBuilder 的 command() 方法 1.前言 在Java开发中,使用Spring Boot时,有时需要执行外部命令或者系统进程。Java提供ProcessBuilder类来启动外部进程,并且其command 不念不忘少年蓝@/ 2024年02月05日 13:58/ 0 赞/ 48 阅读
相关 Java ProcessBuilder类学习 文章目录 一、ProcessBuilder基础 1、ProcessBuilder类 1.1 概述 1.2 进程 怼烎@/ 2023年09月30日 13:48/ 0 赞/ 25 阅读
相关 java ProcessBuilder 文章目录 java ProcessBuilder code java ProcessBuilder code public 旧城等待,/ 2023年06月27日 03:08/ 0 赞/ 34 阅读
相关 Java ProcessBuilder示例 在Java中,我们可以使用`ProcessBuilder`轻松调用外部命令: ProcessBuilder processBuilder = new ProcessB 布满荆棘的人生/ 2023年02月15日 05:10/ 0 赞/ 22 阅读
相关 Java——ProcessBuilder类的使用及获取APK信息 这次是想获取APK包的版本号,包名,大小等信息。其实有两种方法: 一种是: [纯JAVA环境获取APK信息(包名,版本,版本号,大小,权限...),纯JAV 素颜马尾好姑娘i/ 2022年08月05日 12:18/ 0 赞/ 195 阅读
相关 ProcessBuilder 用法详解 原文:https://blog.csdn.net/zhoujing\_0424/article/details/79917368 1. 简介 ProcessBuilde 超、凢脫俗/ 2022年01月06日 14:43/ 0 赞/ 687 阅读
相关 JavaSE_指令_ProcessBuilder JavaSE\_指令\_ProcessBuilder 说明 示例代码 场景 实现 说明 此类用于创建操作系统进程。 每个 ╰半夏微凉°/ 2021年10月06日 03:50/ 0 赞/ 318 阅读
还没有评论,来说两句吧...