这里面我们对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类的使用(一)的更多相关文章

  1. 深入研究java.lang.Runtime类【转】

    转自:http://blog.csdn.net/lastsweetop/article/details/3961911 目录(?)[-] javalang 类 Runtime getRuntime e ...

  2. java基础-System类常用方法介绍

    java基础-System类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.System类概念 在API中system类介绍的比较简单,我们给出定义,system中 ...

  3. 浅析Java.lang.Runtime类

    一.概述      Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接.      一般不能实例化一个Runtime对象, ...

  4. 【转】深入研究java.lang.Runtime类

    一.概述      Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接.      一般不能实例化一个Runtime对象, ...

  5. 第二十九节:Java基础知识-类,多态,Object,数组和字符串

    前言 Java基础知识-类,多态,Object,数组和字符串,回顾,继承,类的多态性,多态,向上转型和向下转型,Object,数组,多维数组,字符串,字符串比较. 回顾 类的定义格式: [类的修饰符] ...

  6. java基础-BigDecimal类常用方法介绍

    java基础-BigDecimal类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.BigDecimal类概述 我们知道浮点数的计算结果是未知的.原因是计算机二进制 ...

  7. java基础-BigInteger类常用方法介绍

    java基础-BigInteger类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.BigInteger类概述 Java中long型为最大整数类型,对于超过long ...

  8. java基础-Arrays类常用方法介绍

    java基础-Arrays类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Array类的概念 此类包含用来操作数组(比如排序和搜索)的各种方法.需要注意,如果指定 ...

  9. java基础-Math类常用方法介绍

    java基础-Math类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Math类概念 Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函 ...

随机推荐

  1. e740. 向标签中加入一个图标

    This example creates a JLabel component with an icon. // Fetch icon Icon icon = new ImageIcon(" ...

  2. e587. Filling Basic Shapes

    There are two ways to fill basic shapes like lines and rectangles. The first is to use specific draw ...

  3. TCDB 数据库简介

    TCDB是对膜转运蛋白(Membrane Transport Protein)进行分类的一个数据库,它制定了一套转运蛋白分类系统(Transporter Classification), 简称TC S ...

  4. mysql错误代码对照表较完整 mysql_errno()

    From: http://blog.csdn.net/aidenliu/article/details/5925604 mysql错误代码对照表较完整  0101 属于其他进程的专用标志. 0102 ...

  5. html 禁用点击事件

    1.css禁用鼠标点击事件 .disabled { pointer-events: none; } 注:(这个没有试过) jquery禁用a标签方法1 $(document).ready(functi ...

  6. iOS 多线程简单使用的具体解释

    主线程 一个iOS程序执行后.默认会开启1条线程,称为"主线程"或"UI线程"(刷新UI界面最好在主线程中做.在子线程中可能会出现莫名其妙的BUG) 主线程的作 ...

  7. 通过tarball形式安装HBASE Cluster(CDH5.0.2)——配置分布式集群中的YARN ResourceManager 的HA

    <?xml version="1.0"?> <!-- Licensed under the Apache License, Version 2.0 (the &q ...

  8. 修改vs2005,vs2008,vs2010调试默认浏览器

    前些日子不小心安装上了一个sogou的浏览器,感觉这个浏览器用起来也算方便,所以就么有卸载,一直就这么用着,但当我用vs来调试web程序的 时候问题出来了,默认的调试浏览器变成了搜狗的浏览器了,我在v ...

  9. 如何用BarTender将日期变量和序列号变量放一起打印成条码?

    刚接触BarTender 2016的小伙伴们可能对条码的数据源还不太搞的定,例如有时需要将日期变量和序列号变量放一起打印成条码,那如何简单达到目的呢?下面,小编教大家解决这一问题的三大步骤. 1.在B ...

  10. VS或编译的时候不生成Release文件夹

    今天在编译第三方类的时候,总是发布的时候报没有第三方类库的的Release版本 解决方案: Build=>Configuration Manager=>Release 编译=>配置管 ...