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

Runtimeo类的使用

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

  1. @Test
  2. public void runtimeInfo() {
  3. Runtime runtime = Runtime.getRuntime();
  4. int processors = runtime.availableProcessors();
  5. long freeMemory = runtime.freeMemory();
  6. long maxMemory = runtime.maxMemory();
  7. long totalMemory = runtime.totalMemory();
  8.  
  9. // processors=4, freeMemory=165713400, maxMemory=2837446656, totalMemory=192937984
  10. logger.debug("processors={}, freeMemory={}, maxMemory={}, totalMemory={}", processors, freeMemory, maxMemory, totalMemory);
  11. }

二、得到系统的环境变量

  1. @Test
  2. public void dirRuntimeProcess() throws IOException, InterruptedException {
  3. Process process = Runtime.getRuntime().exec("cmd.exe /c echo %JAVA_HOME%");
  4. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
  5.  
  6. String string = null;
  7. while ((string = bufferedReader.readLine()) != null) {
  8. System.out.println(string); // D:\Java\jdk\jdk1.8.0_152
  9. }
  10. process.waitFor();
  11. System.out.println("return: " + process.exitValue()); // return: 0
  12. }

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

  1. @Test
  2. public void getJavaVersion() {
  3. try {
  4. Process process = Runtime.getRuntime().exec("javac -version");
  5. BufferedReader br = new BufferedReader(new InputStreamReader(process.getErrorStream()));
  6. String line = null;
  7. while ((line = br.readLine()) != null)
  8. System.out.println(line); // javac 1.8.0_152
  9. process.waitFor();
  10. System.out.println("Process exitValue: " + process.exitValue());
  11. } catch (Throwable t) {
  12. t.printStackTrace();
  13. }
  14. }

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

  1. @Test
  2. public void execProgramC() {
  3. try {
  4. Process process = Runtime.getRuntime().exec("C:/Users/76801/Desktop/huhx.exe");
  5. BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
  6. String line = null;
  7. while ((line = br.readLine()) != null)
  8. System.out.println(line); // Hello World.
  9. process.waitFor();
  10. System.out.println("Process exitValue: " + process.exitValue());
  11. } catch (Throwable t) {
  12. t.printStackTrace();
  13. }
  14. }

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

  1. #include<stdio.h>
  2.  
  3. void main() {
  4. printf("Hello World.");
  5. }

五、使用Runtime类导出mysql脚本

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

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

  1. public class StreamGobbler extends Thread {
  2.  
  3. InputStream is;
  4. String type;
  5.  
  6. public StreamGobbler(InputStream is, String type) {
  7. this.is = is;
  8. this.type = type;
  9. }
  10.  
  11. public void run() {
  12. try (InputStreamReader isr = new InputStreamReader(is);) {
  13. BufferedReader br = new BufferedReader(isr);
  14. String line = null;
  15. while ((line = br.readLine()) != null) {
  16. if (type.equals("Error")) {
  17. System.out.println("Error :" + line);
  18. } else {
  19. System.out.println("Debug:" + line);
  20. }
  21. }
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. }

代码的目标是导出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. Python:数组、队列及堆栈的使用(list用法)--转

    Python编程中数组.队列及堆栈用于保存一组数据或对象的序列,元素可以是各种类型混合在一起,定义格式为[元素,元素,……,元素],用变量[位置]即可取出相应的元素,其中“位置”是从零开始计算. 数组 ...

  2. The required Server component failed to start so Tomcat is unable to start问题解决

    问题出现: Server Tomcat v8.5 Server at localhost failed to start.  或者The required Server component faile ...

  3. 命令查询职责分离模式(Command Query Responsibility Segregation,CQRS)

    浅谈命令查询职责分离(CQRS)模式 CQRS架构简介 对CQRS的一次批判性思考

  4. 【Php】数组遍历,foreach, each, trim()

    <?php $iplist = "122.224.251.154|192.168.2.138|192.168.2.12"; echo $_SERVER['REMOTE_ADD ...

  5. docker默认ip查询

    查询docker ip地址 docker-machine ip default

  6. 学习TensorFlow的tf.concat使用

    https://www.tensorflow.org/api_docs/python/tf/concat

  7. u3d fpsCounter

    因为u3d自己的stats下面的fpscounter不是实际意义上的fps,所以看到demo的fpsCounter,把它改写为c#的 using UnityEngine;using System.Co ...

  8. Thinkphp5笔记五:配置data文件夹

    如果你看项目下的各种文件,有种乱七八糟的感觉的话,你就可以进行以下配置. 配置data文件夹的,整理各种文件,让看起来舒服些. 一.设置runtime文件夹 index.php define('RUN ...

  9. mint-ui loadmore组件注意问题

    loadTop(){ this.$store.dispatch('getNewsList',{channelId:this.id,page:0,size:this.size}); this.$refs ...

  10. python获取两个dict的不同

    参数: dict1, dict2 需求:如果dict1和dict2中有不同的key,那么返回这个(key, dict1[key]):如果dict1和dict2中有相同的key,但是value不同,返回 ...