public class RuntimeUtil extends Object
Constructor and Description |
---|
RuntimeUtil() |
Modifier and Type | Method and Description |
---|---|
static void |
addShutdownHook(Runnable hook)
增加一个JVM关闭后的钩子,用于在JVM关闭时执行某些操作
|
static void |
destroy(Process process)
销毁进程
|
static Process |
exec(String... cmds)
执行命令
命令带参数时参数可作为其中一个参数,也可以将命令和参数组合为一个字符串传入 |
static Process |
exec(String[] envp,
File dir,
String... cmds)
执行命令
命令带参数时参数可作为其中一个参数,也可以将命令和参数组合为一个字符串传入 |
static Process |
exec(String[] envp,
String... cmds)
执行命令
命令带参数时参数可作为其中一个参数,也可以将命令和参数组合为一个字符串传入 |
static List<String> |
execForLines(Charset charset,
String... cmds)
执行系统命令,使用系统默认编码
|
static List<String> |
execForLines(String... cmds)
执行系统命令,使用系统默认编码
|
static String |
execForStr(Charset charset,
String... cmds)
执行系统命令,使用系统默认编码
|
static String |
execForStr(String... cmds)
执行系统命令,使用系统默认编码
|
static String |
getErrorResult(Process process)
获取命令执行异常结果,使用系统默认编码,,获取后销毁进程
|
static String |
getErrorResult(Process process,
Charset charset)
获取命令执行异常结果,获取后销毁进程
|
static long |
getFreeMemory()
获得JVM中剩余的内存数,单位byte
|
static long |
getMaxMemory()
获得JVM中可以从系统中获取的最大的内存数,单位byte,以-Xmx参数为准
|
static int |
getProcessorCount()
获得JVM可用的处理器数量(一般为CPU核心数)
|
static String |
getResult(Process process)
获取命令执行结果,使用系统默认编码,,获取后销毁进程
|
static String |
getResult(Process process,
Charset charset)
获取命令执行结果,获取后销毁进程
|
static List<String> |
getResultLines(Process process)
获取命令执行结果,使用系统默认编码,获取后销毁进程
|
static List<String> |
getResultLines(Process process,
Charset charset)
获取命令执行结果,使用系统默认编码,获取后销毁进程
|
static long |
getTotalMemory()
获得JVM已经从系统中获取到的总共的内存数,单位byte
|
static long |
getUsableMemory()
获得JVM最大可用内存,计算方法为:
最大内存-总内存+剩余内存 |
public static String execForStr(String... cmds) throws IORuntimeException
cmds
- 命令列表,每个元素代表一条命令IORuntimeException
- IO异常public static String execForStr(Charset charset, String... cmds) throws IORuntimeException
charset
- 编码cmds
- 命令列表,每个元素代表一条命令IORuntimeException
- IO异常public static List<String> execForLines(String... cmds) throws IORuntimeException
cmds
- 命令列表,每个元素代表一条命令IORuntimeException
- IO异常public static List<String> execForLines(Charset charset, String... cmds) throws IORuntimeException
charset
- 编码cmds
- 命令列表,每个元素代表一条命令IORuntimeException
- IO异常public static Process exec(String... cmds)
cmds
- 命令Process
public static Process exec(String[] envp, String... cmds)
envp
- 环境变量参数,传入形式为key=value,null表示继承系统环境变量cmds
- 命令Process
public static Process exec(String[] envp, File dir, String... cmds)
envp
- 环境变量参数,传入形式为key=value,null表示继承系统环境变量dir
- 执行命令所在目录(用于相对路径命令执行),null表示使用当前进程执行的目录cmds
- 命令Process
public static List<String> getResultLines(Process process)
process
- Process
进程public static List<String> getResultLines(Process process, Charset charset)
process
- Process
进程charset
- 编码public static String getResult(Process process)
process
- Process
进程public static String getResult(Process process, Charset charset)
process
- Process
进程charset
- 编码public static String getErrorResult(Process process)
process
- Process
进程public static String getErrorResult(Process process, Charset charset)
process
- Process
进程charset
- 编码public static void destroy(Process process)
process
- 进程public static void addShutdownHook(Runnable hook)
hook
- 钩子public static int getProcessorCount()
public static long getFreeMemory()
public static long getTotalMemory()
public static long getMaxMemory()
public static long getUsableMemory()
Copyright © 2021. All rights reserved.