码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-12-26 17:59:02      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:public   system   roc   std   learn   output   lin   dir   obb   

  这里面我们对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。

 

友情链接

 

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

标签:public   system   roc   std   learn   output   lin   dir   obb   

原文地址:https://www.cnblogs.com/huhx/p/baseusejavaruntime1.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!