错误记录Android 中调用 Process 命令行执行指令 ( java.lang.IllegalThreadStateException: process hasn‘t exited )(代码片

Posted 韩曙亮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了错误记录Android 中调用 Process 命令行执行指令 ( java.lang.IllegalThreadStateException: process hasn‘t exited )(代码片相关的知识,希望对你有一定的参考价值。





一、报错信息



2021-05-07 13:24:24.672 29512-29512/kim.hsl.a7_zip E/androidRuntime: FATAL EXCEPTION: main
    Process: kim.hsl.a7_zip, PID: 29512
    java.lang.RuntimeException: Unable to start activity ComponentInfo{kim.hsl.a7_zip/kim.hsl.a7_zip.MainActivity}: java.lang.IllegalThreadStateException: process hasn't exited
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2951)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6718)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.IllegalThreadStateException: process hasn't exited
        at java.lang.UNIXProcess.exitValue(UNIXProcess.java:215)
        at kim.hsl.a7_zip.MainActivity.compress7z(MainActivity.kt:115)
        at kim.hsl.a7_zip.MainActivity.onCreate(MainActivity.kt:24)
        at android.app.Activity.performCreate(Activity.java:7144)
        at android.app.Activity.performCreate(Activity.java:7135)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2931)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6718) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

在这里插入图片描述

问题描述 : 使用 Runtime.getRuntime().exec(cmd) 执行命令 , 如果实时打印出命令执行的命令行信息 , 使用如下代码 :

        var process: Process = Runtime.getRuntime().exec(cmd)

        // 读取命令执行过程数据
        var reader = BufferedReader(InputStreamReader(process.inputStream))
        while (true) {
            val line = reader.readLine()
            if (line != null) {
                Log.i(TAG, "$line")
            }else{
                break
            }
        }

执行完毕后 , 调用如下代码查询执行结果 , 报上述错误 ;

process.exitValue()




二、解决方案



出现问题原因 : 读取命令执行过程的 BufferedReader 没有关闭 ;

正确用法 :

        var process: Process = Runtime.getRuntime().exec(cmd)

        // 读取命令执行过程数据
        var reader = BufferedReader(InputStreamReader(process.inputStream))
        while (true) {
            val line = reader.readLine()
            if (line != null) {
                Log.i(TAG, "$line")
            }else{
                break
            }
        }
        reader.close()

        val exitValue = process.exitValue()

使用 BufferedReader 读取命令行执行过程 , 读取完毕后 , 及时关闭 BufferedReader ;

以上是关于错误记录Android 中调用 Process 命令行执行指令 ( java.lang.IllegalThreadStateException: process hasn‘t exited )(代码片的主要内容,如果未能解决你的问题,请参考以下文章

process.start启动exe,程序中会调用配置文件的路径是相对路径,产生启动错误,怎么办?

[Java][Android][Process] ProcessBuilder与Runtime区别

错误记录Android Studio 编译报错 ( A problem occurred starting process ‘command ‘ninja.exe‘ ‘ )

错误记录Android 文件分享 FileProvider 设置错误

如何修复错误:java.util.concurrent.ExecutionException:com.android.ide.common.process.ProcessException:

错误:任务“:app:dexDebug”执行失败。 com.android.ide.common.process.ProcessException