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。
友情链接
java基础---->Runtime类的使用(一)的更多相关文章
- 深入研究java.lang.Runtime类【转】
转自:http://blog.csdn.net/lastsweetop/article/details/3961911 目录(?)[-] javalang 类 Runtime getRuntime e ...
- java基础-System类常用方法介绍
java基础-System类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.System类概念 在API中system类介绍的比较简单,我们给出定义,system中 ...
- 浅析Java.lang.Runtime类
一.概述 Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接. 一般不能实例化一个Runtime对象, ...
- 【转】深入研究java.lang.Runtime类
一.概述 Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接. 一般不能实例化一个Runtime对象, ...
- 第二十九节:Java基础知识-类,多态,Object,数组和字符串
前言 Java基础知识-类,多态,Object,数组和字符串,回顾,继承,类的多态性,多态,向上转型和向下转型,Object,数组,多维数组,字符串,字符串比较. 回顾 类的定义格式: [类的修饰符] ...
- java基础-BigDecimal类常用方法介绍
java基础-BigDecimal类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.BigDecimal类概述 我们知道浮点数的计算结果是未知的.原因是计算机二进制 ...
- java基础-BigInteger类常用方法介绍
java基础-BigInteger类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.BigInteger类概述 Java中long型为最大整数类型,对于超过long ...
- java基础-Arrays类常用方法介绍
java基础-Arrays类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Array类的概念 此类包含用来操作数组(比如排序和搜索)的各种方法.需要注意,如果指定 ...
- java基础-Math类常用方法介绍
java基础-Math类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Math类概念 Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函 ...
随机推荐
- Python:数组、队列及堆栈的使用(list用法)--转
Python编程中数组.队列及堆栈用于保存一组数据或对象的序列,元素可以是各种类型混合在一起,定义格式为[元素,元素,……,元素],用变量[位置]即可取出相应的元素,其中“位置”是从零开始计算. 数组 ...
- 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 ...
- 命令查询职责分离模式(Command Query Responsibility Segregation,CQRS)
浅谈命令查询职责分离(CQRS)模式 CQRS架构简介 对CQRS的一次批判性思考
- 【Php】数组遍历,foreach, each, trim()
<?php $iplist = "122.224.251.154|192.168.2.138|192.168.2.12"; echo $_SERVER['REMOTE_ADD ...
- docker默认ip查询
查询docker ip地址 docker-machine ip default
- 学习TensorFlow的tf.concat使用
https://www.tensorflow.org/api_docs/python/tf/concat
- u3d fpsCounter
因为u3d自己的stats下面的fpscounter不是实际意义上的fps,所以看到demo的fpsCounter,把它改写为c#的 using UnityEngine;using System.Co ...
- Thinkphp5笔记五:配置data文件夹
如果你看项目下的各种文件,有种乱七八糟的感觉的话,你就可以进行以下配置. 配置data文件夹的,整理各种文件,让看起来舒服些. 一.设置runtime文件夹 index.php define('RUN ...
- mint-ui loadmore组件注意问题
loadTop(){ this.$store.dispatch('getNewsList',{channelId:this.id,page:0,size:this.size}); this.$refs ...
- python获取两个dict的不同
参数: dict1, dict2 需求:如果dict1和dict2中有不同的key,那么返回这个(key, dict1[key]):如果dict1和dict2中有相同的key,但是value不同,返回 ...