java基础---->Runtime类的使用(一)

  这里面我们对java中的Runtime类做一个简单的了解介绍。若不常想到无常和死,虽有绝顶的聪明,照理说也和呆子一样。

 

Runtimeo类的使用

一、得到系统内存的一些信息

@Test
public void runtimeInfo() {
    Runtime runtime = Runtime.getRuntime();
    int processors = runtime.availableProcessors();
    long freeMemory = runtime.freeMemory();
    long maxMemory = runtime.maxMemory();
    long totalMemory = runtime.totalMemory();

    // processors=4, freeMemory=165713400, maxMemory=2837446656, totalMemory=192937984
    logger.debug("processors={}, freeMemory={}, maxMemory={}, totalMemory={}", processors, freeMemory, maxMemory, totalMemory);
}

 

二、得到系统的环境变量

@Test
public void dirRuntimeProcess() throws IOException, InterruptedException {
    Process process = Runtime.getRuntime().exec("cmd.exe /c echo %JAVA_HOME%");
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

    String string = null;
    while ((string = bufferedReader.readLine()) != null) {
        System.out.println(string); // D:\Java\jdk\jdk1.8.0_152
    }
    process.waitFor();
    System.out.println("return: " + process.exitValue()); // return: 0
}

 

三、得到java的版本号,这个和上述的不一样

@Test
public void getJavaVersion() {
    try {
        Process process = Runtime.getRuntime().exec("javac -version");
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        String line = null;
        while ((line = br.readLine()) != null)
            System.out.println(line); // javac 1.8.0_152
        process.waitFor();
        System.out.println("Process exitValue: " + process.exitValue());
    } catch (Throwable t) {
        t.printStackTrace();
    }
}

 

四、执行外部命令得到的结果

@Test
public void execProgramC() {
    try {
        Process process = Runtime.getRuntime().exec("C:/Users/76801/Desktop/huhx.exe");
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = null;
        while ((line = br.readLine()) != null)
            System.out.println(line); // Hello World.
        process.waitFor();
        System.out.println("Process exitValue: " + process.exitValue());
    } catch (Throwable t) {
        t.printStackTrace();
    }
}

huhx.c比较简单,就是打印一句话。

#include<stdio.h>

void main() {
    printf("Hello World.");
}

 

五、使用Runtime类导出mysql脚本

@Test
public void execMysqldump() throws IOException, InterruptedException {
    String execCommand = "cmd c/ D:/Java/mysqldump.exe -uhuhx -phuhx boot_learn > D:/bootlearn.sql";
    System.out.println("exec command: " + execCommand);
    Runtime runtime = Runtime.getRuntime();
    Process p = runtime.exec(execCommand);
    StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "Error");
    StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), "Output");
    errorGobbler.start();
    outputGobbler.start();
    p.waitFor();
    System.out.println("successful." + p.exitValue());
}

上述也使用到了网上所说的读出窗口的标准输出缓冲区中的内容,仍旧没有解决Process的waitFor阻塞问题。下面是清空缓冲区的线程代码:

public class StreamGobbler extends Thread {

    InputStream is;
    String type;

    public StreamGobbler(InputStream is, String type) {
        this.is = is;
        this.type = type;
    }

    public void run() {
        try (InputStreamReader isr = new InputStreamReader(is);) {
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                if (type.equals("Error")) {
                    System.out.println("Error   :" + line);
                } else {
                    System.out.println("Debug:" + line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 代码的目标是导出mysql数据库的脚本。没有找到问题的解决方案,运行环境是win10,jdk1.8。

 

友情链接

 

转载于:https://www.cnblogs.com/huhx/p/baseusejavaruntime1.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值