java 常用类库:操作系统System类,运行时环境Runtime

时间:2023-03-08 19:16:15

System类:

System 类代表Java程序的运行平台,程序不能创建System类的对象, System类提供了一些类变量和类方法,允许直接通过 System 类来调用这些类变量和类方法。
System 类提供了代表标准输入、标准输出和错误输出的类变量,并提供了一些静态方法用于访问环境变量、系统属性的方法,还提供了加载文件和动态链接库的方法。

获取操作系统环境变量System.getenv()

package com.zmd.common_class_libraries;

import java.util.Map;

/**
* @ClassName SystemExample
* @projectName: object1
* @author: Zhangmingda
* @description: XXX
* date: 2021/4/6.
*/
public class SystemExample {
public static void main(String[] args) {
Map<String, String> envMap = System.getenv();
for (String key : envMap.keySet()) {
String value = envMap.get(key);
System.out.println(key + "=" + value);
}
System.out.println("-------------------------");
System.out.println(System.getenv("JAVA_HOME"));
}
}

java 常用类库:操作系统System类,运行时环境Runtime

获取操作系统的运行时环境Runtime

Runtime runtime = Runtime.getRuntime(); 不用new 直接引用静态方法即可

获取CPU核心数、runtime.availableProcessors()

package com.zmd.common_class_libraries;

import java.io.IOException;

/**
* @ClassName RuntimeExample
* @projectName: object1
* @author: Zhangmingda
* @description: XXX
* date: 2021/4/6.
*/
public class RuntimeExample {
public static void main(String[] args) throws IOException {
//不用new关键字,直接用类的静态方法,获取当前运行时环境
Runtime runtime = Runtime.getRuntime();
//获取CPU核心数
System.out.println("处理器数量:" + runtime.availableProcessors());
//获取可用最大内存
System.out.println("最大可用内存:" + runtime.maxMemory());
//获取java虚拟机的总内存大小
System.out.println("JAVA虚拟机总内存totalMemeory:" + runtime.totalMemory());
//空闲内存
System.out.println("空闲内存:" + runtime.freeMemory()); //通过java启动系统exe文件例如QQ音乐
runtime.exec("\"C:\\Program Files (x86)\\Tencent\\QQMusic\\QQMusic.exe\""); }
}

通过java启动应用程序runtime.exec("xxx/xx/x.exe");