JAVA基础 之 Runtime

简要说明

    1.Runtime类是java程序的运行时环境

    2.通过Runtime.getRuntime() 获得当前的运行环境实例

    3.程序不能主动创建自己的Runtime实例

    4.功能:能访问jvm相关信息,最重要的是它可以执行命令行

    5.新鲜出炉的应用实例:《JAVA应用 之 快捷工具托盘》

 

 

package com.cxy.f;

import java.io.IOException;

public class RuntimeTest
{
	/**
	 * 1.java程序的运行时环境
	 * 2.通过Runtime.getRuntime() 获得当前的运行环境实例
	 * 3.程序不能创建自己的Runtime实例
	 * 4.能访问jvm相关信息,最重要的是它可以执行命令行
	 */
	public static void main(String[] args) throws IOException
	{
		//获得当前Runtime实例
		Runtime rt=Runtime.getRuntime();
		
		System.out.println("可用处理器个数:"+rt.availableProcessors());
		System.out.println("可用最大内存:"+rt.maxMemory());
		System.out.println("当前总内存:"+rt.totalMemory());
		System.out.println("空闲内存:"+rt.freeMemory());
		System.out.println("-----------------------------");
		
		//重点方法 运行命令行 (notepad.exe、calc.exe、某个可执行文件的路径)
		String command="calc.exe";  //打开计算器
		rt.exec(command);
		//打开QQ 请改成自己QQ的路径
		command="C:\\Program Files (x86)\\Tencent\\QQ\\QQProtect\\Bin\\QQProtect.exe";  
		rt.exec(command);
		command="cmd /c md d:\\cxyCommandTest";  //D盘下创建一个cxyCommandTest文件夹
		rt.exec(command);
		
		//和System类似的方法,实际上System中调用的就是Runtime对应的方法
		rt.runFinalization();  //运行finalize方法。
		rt.gc();  //运行垃圾回收器(官方解释) 实际上这种运行可能只是建议性运行
		rt.exit(0);  //参数 如果是非0代表异常终止,多放在捕获异常的catch块中
	}
}

 

 

说明:

    1.更多常用命令 请参看 windows7 常用简单命令总结》

    2.原创文章,转载请标明并加本文连接。

    3.更详尽的API请参见  http://docs.oracle.com/javase/7/docs/api/

    4.文章反映个人愚见,如有异议欢迎讨论指正 

package com.test; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class B { public static void main(String[] args) { File file = new File("c:\\a.jpg"); FileInputStream is = null; try { is = new FileInputStream(file); } catch (FileNotFoundException e2) { e2.printStackTrace(); // return rect; } BufferedImage sourceImg = null; try { sourceImg = javax.imageio.ImageIO.read(is); } catch (IOException e1) { e1.printStackTrace(); // return rect; } System.out.println("width = " + sourceImg.getWidth() + "height = " + sourceImg.getHeight()); } } package com.test; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class B { public static void main(String[] args) { File file = new File("c:\\a.jpg"); FileInputStream is = null; try { is = new FileInputStream(file); } catch (FileNotFoundException e2) { e2.printStackTrace(); // return rect; } BufferedImage sourceImg = null; try { sourceImg = javax.imageio.ImageIO.read(is); } catch (IOException e1) { e1.printStackTrace(); // return rect; } System.out.println("width = " + sourceImg.getWidth() + "height = " + sourceImg.getHeight()); } } package com.test; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class B { public static void main(String[] args) { File file = new File("c:\\a.jpg"); FileInputStream is = null; try { is = new FileInputStream(file); } catch (FileNotFoundException e2) { e2.printStackTrace(); // return rect; } BufferedImage sourceImg = null; try { sourceImg = javax.imageio.ImageIO.read(is); } catch (IOException e1) { e1.printStackTrace(); // return rect; } System.out.println("width = " + sourceImg.getWidth() + "height = " + sourceImg.getHeight()); } } package com.test; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class B { public static void main(String[] args) { File file = new File("c:\\a.jpg"); FileInputStream is = null; try { is = new FileInputStream(file); } catch (FileNotFoundException e2) { e2.printStackTrace(); // return rect; } BufferedImage sourceImg = null; try { sourceImg = javax.imageio.ImageIO.read(is); } catch (IOException e1) { e1.printStackTrace(); // return rect; } System.out.println("width = " + sourceImg.getWidth() + "height = " + sourceImg.getHeight()); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值