RunTime详解,java获取内存信息,获取cpu信息,执行cmd命令,执行dos命令,执行git命令,执行java命令

package top.lishuoboy.java_basic.runtime;

import java.io.*;

/**
 * 执行各种命令
 *
 * @Author top.lishuoboy
 * @Date 2019/10/10 18:23
 * @Version 1.0
 */
public class RunTimeTest {
    private static Runtime runtime = Runtime.getRuntime();

    public static void main(String[] args) throws Throwable {

        runtime.gc();   // 就是 System.gc();
        getMemoryCpu();
        System.out.println("====================================================================");
        execTestEasy();
        System.out.println("====================================================================");
        execTestHard();
        System.out.println("====================================================================");
        execTestGit();

    }


    private static void getMemoryCpu() {
        System.out.println(runtime.availableProcessors());
        System.out.println(runtime.freeMemory());
        System.out.println(runtime.maxMemory());
        System.out.println(runtime.totalMemory());
    }

    /**
     * 执行cmd命令
     *
     * @throws IOException
     */
    private static void execTestEasy() throws IOException {
        /*
            cmd /c dir 是执行完dir命令后关闭命令窗口。
            cmd /k dir 是执行完dir命令后不关闭命令窗口。
            cmd /c start dir 会打开一个新窗口后执行dir指令,原窗口会关闭。
            cmd /k start dir 会打开一个新窗口后执行dir指令,原窗口不会关闭。
         */
        Process process = runtime.exec("cmd /c dir", null, new File("D:\\111"));
        InputStream is = process.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "GBK"));

        String line = br.readLine();
        while (line != null) {
            System.out.println(line);
            line = br.readLine();
        }

        System.out.println("end");

    }

    static void execTestHard() throws IOException {
        /*
            cmd /c dir 是执行完dir命令后关闭命令窗口。
            cmd /k dir 是执行完dir命令后不关闭命令窗口。
            cmd /c start dir 会打开一个新窗口后执行dir指令,原窗口会关闭。
            cmd /k start dir 会打开一个新窗口后执行dir指令,原窗口不会关闭。
         */
        String[] commamd = new String[]{"cmd", "/c", "echo", "%a%", "%b%"};
        String[] envp = new String[]{"a=aaa", "b=bbb"};
        Process process = runtime.exec(commamd, envp);
        InputStream is = process.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "GBK"));

        String line = br.readLine();
        while (line != null) {
            System.out.println(line);
            line = br.readLine();
        }

        System.out.println("end");
    }


    /**
     * 执行git命令 ,执行git命令
     *
     * @throws IOException
     */
    private static void execTestGit() throws IOException {
        Process process = runtime.exec("git config --global -l");
        InputStream is = process.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "GBK"));

        String line = br.readLine();
        while (line != null) {
            System.out.println(line);
            line = br.readLine();
        }

        System.out.println("end");

    }
}

 输出

8
188540928
2833776640
191365120
====================================================================
 驱动器 D 中的卷是 程序
 卷的序列号是 CC21-0E69

 D:\111 的目录

2019/09/19  16:52    <DIR>          .
2019/09/19  16:52    <DIR>          ..
2019/03/19  15:45           471,282 111.bmp
2019/03/19  15:46            60,787 111.jpg
2019/03/19  15:46            12,621 111.png
2019/03/19  15:27               460 111.txt
2019/01/23  14:04             1,626 111.war
2019/03/19  15:55           235,411 222.png
2019/03/19  11:26            46,175 aaa.pdf
2019/02/19  11:00    <DIR>          antd-course
2019/01/02  14:24             2,381 baidu.html
2019/03/18  10:19    <DIR>          ES6
2019/03/18  10:08                 0 ES6.txt
2019/03/21  10:00    <DIR>          nodejs
2019/03/21  09:15    <DIR>          office2Pdf
2019/03/22  17:52               217 package.json
2019/03/18  15:15    <DIR>          react
2019/03/15  18:46    <DIR>          webpack
              10 个文件        830,960 字节
               8 个目录 190,255,316,992 可用字节
end
====================================================================
aaa bbb
end
====================================================================
user.email=25135394@qq.com
user.name=LiShuo
gui.recentrepo=D:/Workspace/roface-rdc
gui.recentrepo=D:/Workspace/rober-cabin-rdc
gui.recentrepo=D:/Workspace/antd-course
gui.recentrepo=D:/Workspace/rober-cabin
gui.recentrepo=D:/Workspace/roface
end


 

posted on 2019-10-10 20:05  小石头小祖宗  阅读(20)  评论(0)    收藏  举报  来源

导航