public class RuntimeUtils extends Object
| Constructor and Description |
|---|
RuntimeUtils() |
| Modifier and Type | Method and Description |
|---|---|
static void |
addShutdownHook(Runnable hook)
增加一个JVM关闭后的钩子,用于在JVM关闭时执行某些操作
|
static <T extends Throwable> |
convertFromOrSuppressedThrowable(Throwable throwable,
Class<T> exceptionClass)
转化指定异常为来自或者包含指定异常
|
static <T extends Throwable> |
convertFromOrSuppressedThrowable(Throwable throwable,
Class<T> exceptionClass,
boolean checkCause)
转化指定异常为来自或者包含指定异常
|
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 Throwable |
getCausedBy(Throwable throwable,
Class<? extends Exception>... causeClasses)
获取由指定异常类引起的异常
|
static String |
getErrorResult(Process process)
获取命令执行异常结果,使用系统默认编码,,获取后销毁进程
|
static String |
getErrorResult(Process process,
Charset charset)
获取命令执行异常结果,获取后销毁进程
|
static String |
getMessage(Throwable e)
获得完整消息,包括异常名,消息格式为:{SimpleClassName}: {ThrowableMessage}
|
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 Throwable |
getRootCause(Throwable throwable)
获取异常链中最尾端的异常,即异常最早发生的异常对象
此方法通过调用
Throwable.getCause() 直到没有cause为止,如果异常本身没有cause,返回异常本身
传入null返回也为null |
static String |
getRootCauseMessage(Throwable th)
获取异常链中最尾端的异常的消息,
消息格式为:{SimpleClassName}: {ThrowableMessage}
|
static StackTraceElement |
getRootStackElement()
获取入口堆栈信息
|
static String |
getSimpleMessage(Throwable e)
获得消息,调用异常类的getMessage方法
|
static StackTraceElement |
getStackElement(int layers)
获取指定层的堆栈信息
|
static StackTraceElement[] |
getStackElements()
获取当前栈信息
|
static String |
getStackTrace(Throwable throwable)
堆栈转为完整字符串
|
static String |
getStackTrace(Throwable throwable,
int limit)
堆栈转为完整字符串
|
static String |
getStackTrace(Throwable throwable,
int limit,
Map<Character,String> replaceCharToStrMap)
堆栈转为完整字符串
|
static String |
getStackTraceOneLine(Throwable throwable)
堆栈转为单行完整字符串
|
static String |
getStackTraceOneLine(Throwable throwable,
int limit)
堆栈转为单行完整字符串
|
static List<Throwable> |
getThrowableList(Throwable throwable)
获取异常链上所有异常的集合,如果
Throwable 对象没有cause,返回只有一个节点的List
如果传入null,返回空集合 |
static boolean |
isCausedBy(Throwable throwable,
Class<? extends Exception>... causeClasses)
判断是否由指定异常类引起
|
static boolean |
isFromOrSuppressedThrowable(Throwable throwable,
Class<? extends Throwable> exceptionClass)
判断指定异常是否来自或者包含指定异常
|
static boolean |
isFromOrSuppressedThrowable(Throwable throwable,
Class<? extends Throwable> exceptionClass,
boolean checkCause)
判断指定异常是否来自或者包含指定异常
|
static Throwable |
unwrap(Throwable wrapped)
剥离反射引发的InvocationTargetException、
UndeclaredThrowableException中间异常,返回业务本身的异常
|
static <T extends Throwable> |
wrap(Throwable throwable,
Class<T> wrapThrowable)
包装一个异常
|
static void |
wrapAndThrow(Throwable throwable)
|
static RuntimeException |
wrapRuntime(Throwable throwable)
使用运行时异常包装编译异常
|
public static String execForStr(String... cmds) throws InstrumentException
cmds - 命令列表,每个元素代表一条命令InstrumentException - IO异常public static String execForStr(Charset charset, String... cmds) throws InstrumentException
charset - 编码cmds - 命令列表,每个元素代表一条命令InstrumentException - 内部处理异常public static List<String> execForLines(String... cmds) throws InstrumentException
cmds - 命令列表,每个元素代表一条命令InstrumentException - 内部处理异常public static List<String> execForLines(Charset charset, String... cmds) throws InstrumentException
charset - 编码cmds - 命令列表,每个元素代表一条命令InstrumentException - 内部处理异常public static Process exec(String... cmds)
cmds - 命令Processpublic static Process exec(String[] envp, String... cmds)
envp - 环境变量参数,传入形式为key=value,null表示继承系统环境变量cmds - 命令Processpublic static Process exec(String[] envp, File dir, String... cmds)
envp - 环境变量参数,传入形式为key=value,null表示继承系统环境变量dir - 执行命令所在目录(用于相对路径命令执行),null表示使用当前进程执行的目录cmds - 命令Processpublic 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 String getMessage(Throwable e)
e - 异常public static String getSimpleMessage(Throwable e)
e - 异常public static RuntimeException wrapRuntime(Throwable throwable)
如果
throwable - 异常public static <T extends Throwable> T wrap(Throwable throwable, Class<T> wrapThrowable)
T - 对象throwable - 异常wrapThrowable - 包装后的异常类public static void wrapAndThrow(Throwable throwable)
throwable - 异常public static Throwable unwrap(Throwable wrapped)
wrapped - 包装的异常public static StackTraceElement[] getStackElements()
public static StackTraceElement getStackElement(int layers)
layers - 堆栈层级public static StackTraceElement getRootStackElement()
public static String getStackTraceOneLine(Throwable throwable)
throwable - 异常对象public static String getStackTraceOneLine(Throwable throwable, int limit)
throwable - 异常对象limit - 限制最大长度public static String getStackTrace(Throwable throwable)
throwable - 异常对象public static String getStackTrace(Throwable throwable, int limit)
throwable - 异常对象limit - 限制最大长度public static String getStackTrace(Throwable throwable, int limit, Map<Character,String> replaceCharToStrMap)
throwable - 异常对象limit - 限制最大长度replaceCharToStrMap - 替换字符为指定字符串public static boolean isCausedBy(Throwable throwable, Class<? extends Exception>... causeClasses)
throwable - 异常causeClasses - 定义的引起异常的类public static Throwable getCausedBy(Throwable throwable, Class<? extends Exception>... causeClasses)
throwable - 异常causeClasses - 定义的引起异常的类public static boolean isFromOrSuppressedThrowable(Throwable throwable, Class<? extends Throwable> exceptionClass)
throwable - 异常exceptionClass - 定义的引起异常的类public static boolean isFromOrSuppressedThrowable(Throwable throwable, Class<? extends Throwable> exceptionClass, boolean checkCause)
throwable - 异常exceptionClass - 定义的引起异常的类checkCause - 判断causepublic static <T extends Throwable> T convertFromOrSuppressedThrowable(Throwable throwable, Class<T> exceptionClass)
T - 异常类型throwable - 异常exceptionClass - 定义的引起异常的类public static <T extends Throwable> T convertFromOrSuppressedThrowable(Throwable throwable, Class<T> exceptionClass, boolean checkCause)
T - 异常类型throwable - 异常exceptionClass - 定义的引起异常的类checkCause - 判断causepublic static List<Throwable> getThrowableList(Throwable throwable)
Throwable 对象没有cause,返回只有一个节点的List
如果传入null,返回空集合throwable - 异常对象,可以为nullpublic static Throwable getRootCause(Throwable throwable)
Throwable.getCause() 直到没有cause为止,如果异常本身没有cause,返回异常本身
传入null返回也为nullthrowable - 异常对象,可能为nullCopyright © 2020. All rights reserved.