Process类:创建子进程进行输入和输出 --《JAVA编程思想》78

如果你想在 JAVA 中执行其他操作系统的程序,例如:运行 DOS 命令、shell 脚本、exe 文件,只需了解 java.lang.Process 类的用法,上述问题皆可实现。

1.输入

以 Windows 平台为例,来看一个简单的例子:

ProcessBuilder 可用于构建 Process ,其构造方法接收 String 类型的可变参数,如果命令带有多个参数,需分割成多个 String 字符串 ;

getInputStream() 获取进程标准的输出流,再使用 InputStreamReader 读取其中的字符,如果包含中文字符,需要指定编码格式;

BufferedReader 开启缓冲输入,增加读取速度;

public class ProcessDemo {

    public static void command(String command) throws IOException {
        Process process = new ProcessBuilder(command.split(" ")).start();
        //指定编码格式,解决中文乱码问题
        BufferedReader results = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
        String s;
        while ((s = results.readLine()) != null) {
            System.out.println(s);
        }
        //关闭流
        results.close();
        //销毁进程
        process.destroy();
    }

    public static void main(String[] args) throws IOException {
        command("ping www.baidu.com");
    }

}

正在 Ping www.a.shifen.com [14.215.177.39] 具有 32 字节的数据:
来自 14.215.177.39 的回复: 字节=32 时间=16ms TTL=55
来自 14.215.177.39 的回复: 字节=32 时间=16ms TTL=55
来自 14.215.177.39 的回复: 字节=32 时间=16ms TTL=55
来自 14.215.177.39 的回复: 字节=32 时间=16ms TTL=55

14.215.177.39Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 16ms,最长 = 16ms,平均 = 16ms

2.输出

假设我们现在需要往进程中输出信息,则需要使用 getOutputStream() 获取面向进程的输出流;

需要注意的是,写入完成后必须得执行 flush() 和 close() 方法,否则进程无法判断写入是否完成,导致写入失败;

  • 示例
public class ProcessDemo {

    public static void command(String command) throws IOException, InterruptedException {
        Process process = new ProcessBuilder(command.split(" ")).start();
        OutputStream outputStream = process.getOutputStream();
        String str="2021/12/01";
        outputStream.write(str.getBytes());
        outputStream.flush();
        //关闭输出流
        outputStream.close();
        //指定编码格式,解决中文乱码问题
        BufferedReader results = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
        String s;
        while ((s = results.readLine()) != null) {
            System.out.println(s);
        }
        //关闭输入流
        results.close();
        //销毁进程
        process.destroy();
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        command("CMD /C date");
    }

}

当前日期: 2021/12/08 周三 
输入新日期: (年月日) 2021/12/01

本次分享至此结束,希望本文对你有所帮助,若能点亮下方的点赞按钮,在下感激不尽,谢谢您的【精神支持】。

若有任何疑问,也欢迎与我交流,若存在不足之处,也欢迎各位指正!

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
process-exporter 是一个用于将系统进程信息以 Prometheus 格式暴露出去的工具。它可以读取 Java 进程的信息,包括进程 PID、内存使用情况、线程数等等。 以下是使用 process-exporter 监控 Java 进程的步骤: 1. 下载 process-exporter 可以从官网下载最新版本的 process-exporter,也可以从 Github 上下载。 2. 启动 process-exporter 使用以下命令启动 process-exporter: ``` ./process-exporter --config.path=/path/to/process-exporter/config.yml ``` 其中,--config.path 参数指定了 process-exporter 的配置文件路径。 以下是一个示例配置文件: ``` process_names: - name: java cmdline: - "java" - "-jar" - "myapp.jar" metrics: - name: pid help: "Process PID" type: GAUGE value: "{{.PID}}" - name: cpu_percent help: "Process CPU utilization percentage" type: GAUGE value: "{{.CPUPercent}}" - name: memory_rss help: "Process RSS memory usage in bytes" type: GAUGE value: "{{.MemRss}}" - name: memory_vms help: "Process VMS memory usage in bytes" type: GAUGE value: "{{.MemVms}}" - name: threads help: "Process thread count" type: GAUGE value: "{{.Threads}}" ``` 以上配置文件指定了需要监控的进程名为 java,进程命令行参数为 java -jar myapp.jar。metrics 部分定义了需要暴露的指标,包括进程 PID、CPU 利用率、内存使用情况和线程数。 3. 访问 metrics 访问 http://localhost:9256/metrics 就可以看到 process-exporter 暴露出来的 metrics 信息了。可以将该地址配置为 Prometheus 的 target,从而实现对 Java 进程的监控。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BaymaxCS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值