Class RuntimeKit

java.lang.Object
org.aoju.bus.core.toolkit.RuntimeKit

public class RuntimeKit extends Object
系统运行时工具类 用于执行系统命令的工具
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • RuntimeKit

      public RuntimeKit()
  • Method Details

    • execForStr

      public static String execForStr(String... cmds) throws InstrumentException
      执行系统命令,使用系统默认编码
      Parameters:
      cmds - 命令列表,每个元素代表一条命令
      Returns:
      执行结果
      Throws:
      InstrumentException - IO异常
    • execForStr

      public static String execForStr(Charset charset, String... cmds) throws InstrumentException
      执行系统命令,使用传入的 charset 编码
      Parameters:
      charset - 编码
      cmds - 命令列表,每个元素代表一条命令
      Returns:
      执行结果
      Throws:
      InstrumentException - 内部处理异常
    • execForLines

      public static List<String> execForLines(String... cmds) throws InstrumentException
      执行系统命令,使用系统默认编码
      Parameters:
      cmds - 命令列表,每个元素代表一条命令
      Returns:
      执行结果, 按行区分
      Throws:
      InstrumentException - 内部处理异常
    • execForLines

      public static List<String> execForLines(Charset charset, String... cmds) throws InstrumentException
      执行系统命令,使用传入的 charset 编码
      Parameters:
      charset - 编码
      cmds - 命令列表,每个元素代表一条命令
      Returns:
      执行结果, 按行区分
      Throws:
      InstrumentException - 内部处理异常
    • exec

      public static Process exec(String... cmds)
      执行命令 命令带参数时参数可作为其中一个参数,也可以将命令和参数组合为一个字符串传入
      Parameters:
      cmds - 命令
      Returns:
      Process
    • exec

      public static Process exec(String[] envp, String... cmds)
      执行命令 命令带参数时参数可作为其中一个参数,也可以将命令和参数组合为一个字符串传入
      Parameters:
      envp - 环境变量参数,传入形式为key=value,null表示继承系统环境变量
      cmds - 命令
      Returns:
      Process
    • exec

      public static Process exec(String[] envp, File dir, String... cmds)
      执行命令 命令带参数时参数可作为其中一个参数,也可以将命令和参数组合为一个字符串传入
      Parameters:
      envp - 环境变量参数,传入形式为key=value,null表示继承系统环境变量
      dir - 执行命令所在目录(用于相对路径命令执行),null表示使用当前进程执行的目录
      cmds - 命令
      Returns:
      Process
    • getResultLines

      public static List<String> getResultLines(Process process)
      获取命令执行结果,使用系统默认编码,获取后销毁进程
      Parameters:
      process - Process 进程
      Returns:
      命令执行结果列表
    • getResultLines

      public static List<String> getResultLines(Process process, Charset charset)
      获取命令执行结果,使用传入的 charset 编码,获取后销毁进程
      Parameters:
      process - Process 进程
      charset - 编码
      Returns:
      命令执行结果列表
    • getResult

      public static String getResult(Process process)
      获取命令执行结果,使用系统默认编码,获取后销毁进程
      Parameters:
      process - Process 进程
      Returns:
      命令执行结果列表
    • getResult

      public static String getResult(Process process, Charset charset)
      获取命令执行结果,获取后销毁进程
      Parameters:
      process - Process 进程
      charset - 编码
      Returns:
      命令执行结果列表
    • getErrorResult

      public static String getErrorResult(Process process)
      获取命令执行异常结果,使用系统默认编码,,获取后销毁进程
      Parameters:
      process - Process 进程
      Returns:
      命令执行结果列表
    • getErrorResult

      public static String getErrorResult(Process process, Charset charset)
      获取命令执行异常结果,获取后销毁进程
      Parameters:
      process - Process 进程
      charset - 编码
      Returns:
      命令执行结果列表
    • destroy

      public static void destroy(Process process)
      销毁进程
      Parameters:
      process - 进程
    • addShutdownHook

      public static void addShutdownHook(Runnable hook)
      增加一个JVM关闭后的钩子,用于在JVM关闭时执行某些操作
      Parameters:
      hook - 钩子
    • getPid

      public static int getPid()
      获得当前进程的PID 当失败时返回-1
      Returns:
      the int
    • getUpTime

      public static long getUpTime()
      返回应用启动到现在的毫秒数
      Returns:
      the long
    • getVmArguments

      public static String getVmArguments()
      返回输入的JVM参数列表
      Returns:
      the string
    • getMessage

      public static String getMessage(Throwable e)
      获得完整消息,包括异常名,消息格式为:{SimpleClassName}: {ThrowableMessage}
      Parameters:
      e - 异常
      Returns:
      完整消息
    • getSimpleMessage

      public static String getSimpleMessage(Throwable e)
      获得消息,调用异常类的getMessage方法
      Parameters:
      e - 异常
      Returns:
      消息
    • wrapRuntime

      public static RuntimeException wrapRuntime(Throwable throwable)
      使用运行时异常包装编译异常 如果传入参数已经是运行时异常,则直接返回,不再额外包装
      Parameters:
      throwable - 异常
      Returns:
      运行时异常
    • wrapRuntime

      public static RuntimeException wrapRuntime(String message)
      将指定的消息包装为运行时异常
      Parameters:
      message - 异常消息
      Returns:
      运行时异常
    • wrap

      public static <T extends Throwable> T wrap(Throwable throwable, Class<T> wrapThrowable)
      包装一个异常
      Type Parameters:
      T - 被包装的异常类型
      Parameters:
      throwable - 异常
      wrapThrowable - 包装后的异常类
      Returns:
      包装后的异常
    • wrapAndThrow

      public static void wrapAndThrow(Throwable throwable)
      包装异常并重新抛出此异常 RuntimeExceptionError 直接抛出,其它检查异常包装为UndeclaredThrowableException 后抛出
      Parameters:
      throwable - 异常
    • wrapRuntimeAndThrow

      public static void wrapRuntimeAndThrow(String message)
      将消息包装为运行时异常并抛出
      Parameters:
      message - 异常消息
    • unwrap

      public static Throwable unwrap(Throwable wrapped)
      剥离反射引发的InvocationTargetException、 UndeclaredThrowableException中间异常,返回业务本身的异常
      Parameters:
      wrapped - 包装的异常
      Returns:
      剥离后的异常
    • getStackElements

      public static StackTraceElement[] getStackElements()
      获取当前栈信息
      Returns:
      当前栈信息
    • getStackElement

      public static StackTraceElement getStackElement(int layers)
      获取指定层的堆栈信息
      Parameters:
      layers - 堆栈层级
      Returns:
      指定层的堆栈信息
    • getStackElement

      public static StackTraceElement getStackElement(String fqcn, int layers)
      获取指定层的堆栈信息
      Parameters:
      fqcn - 指定类名为基础
      layers - 指定类名的类堆栈相对层数
      Returns:
      指定层的堆栈信息
    • getRootStackElement

      public static StackTraceElement getRootStackElement()
      获取入口堆栈信息
      Returns:
      入口堆栈信息
    • getStackTraceOneLine

      public static String getStackTraceOneLine(Throwable throwable)
      堆栈转为单行完整字符串
      Parameters:
      throwable - 异常对象
      Returns:
      堆栈转为的字符串
    • getStackTraceOneLine

      public static String getStackTraceOneLine(Throwable throwable, int limit)
      堆栈转为单行完整字符串
      Parameters:
      throwable - 异常对象
      limit - 限制最大长度
      Returns:
      堆栈转为的字符串
    • getStackTrace

      public static String getStackTrace(Throwable throwable)
      堆栈转为完整字符串
      Parameters:
      throwable - 异常对象
      Returns:
      堆栈转为的字符串
    • getStackTrace

      public static String getStackTrace(Throwable throwable, int limit)
      堆栈转为完整字符串
      Parameters:
      throwable - 异常对象
      limit - 限制最大长度
      Returns:
      堆栈转为的字符串
    • getStackTrace

      public static String getStackTrace(Throwable throwable, int limit, Map<Character,String> replaceCharToStrMap)
      堆栈转为完整字符串
      Parameters:
      throwable - 异常对象
      limit - 限制最大长度,>0表示不限制长度
      replaceCharToStrMap - 替换字符为指定字符串
      Returns:
      堆栈转为的字符串
    • isCausedBy

      public static boolean isCausedBy(Throwable throwable, Class<? extends Exception>... causeClasses)
      判断是否由指定异常类引起
      Parameters:
      throwable - 异常
      causeClasses - 定义的引起异常的类
      Returns:
      是否由指定异常类引起
    • getCausedBy

      public static Throwable getCausedBy(Throwable throwable, Class<? extends Exception>... causeClasses)
      获取由指定异常类引起的异常
      Parameters:
      throwable - 异常
      causeClasses - 定义的引起异常的类
      Returns:
      是否由指定异常类引起
    • isFromOrSuppressedThrowable

      public static boolean isFromOrSuppressedThrowable(Throwable throwable, Class<? extends Throwable> exceptionClass)
      判断指定异常是否来自或者包含指定异常
      Parameters:
      throwable - 异常
      exceptionClass - 定义的引起异常的类
      Returns:
      true 来自或者包含
    • isFromOrSuppressedThrowable

      public static boolean isFromOrSuppressedThrowable(Throwable throwable, Class<? extends Throwable> exceptionClass, boolean checkCause)
      判断指定异常是否来自或者包含指定异常
      Parameters:
      throwable - 异常
      exceptionClass - 定义的引起异常的类
      checkCause - 判断cause
      Returns:
      true 来自或者包含
    • convertFromOrSuppressedThrowable

      public static <T extends Throwable> T convertFromOrSuppressedThrowable(Throwable throwable, Class<T> exceptionClass)
      转化指定异常为来自或者包含指定异常
      Type Parameters:
      T - 异常类型
      Parameters:
      throwable - 异常
      exceptionClass - 定义的引起异常的类
      Returns:
      结果为null 不是来自或者包含
    • convertFromOrSuppressedThrowable

      public static <T extends Throwable> T convertFromOrSuppressedThrowable(Throwable throwable, Class<T> exceptionClass, boolean checkCause)
      转化指定异常为来自或者包含指定异常
      Type Parameters:
      T - 异常类型
      Parameters:
      throwable - 异常
      exceptionClass - 定义的引起异常的类
      checkCause - 判断cause
      Returns:
      结果为null 不是来自或者包含
    • getThrowableList

      public static List<Throwable> getThrowableList(Throwable throwable)
      获取异常链上所有异常的集合,如果Throwable 对象没有cause,返回只有一个节点的List 如果传入null,返回空集合
      Parameters:
      throwable - 异常对象,可以为null
      Returns:
      异常链中所有异常集合
    • getRootCause

      public static Throwable getRootCause(Throwable throwable)
      获取异常链中最尾端的异常,即异常最早发生的异常对象 此方法通过调用Throwable.getCause() 直到没有cause为止,如果异常本身没有cause,返回异常本身 传入null返回也为null
      Parameters:
      throwable - 异常对象,可能为null
      Returns:
      最尾端异常, 传入null参数返回也为null
    • getRootCauseMessage

      public static String getRootCauseMessage(Throwable th)
      获取异常链中最尾端的异常的消息, 消息格式为:{SimpleClassName}: {ThrowableMessage}
      Parameters:
      th - 异常
      Returns:
      消息