检查是否安装了Linux "屏幕 "包

问题描述 投票:0回答:1

我需要一种方法来检查Linux屏幕包是否被安装,但要通过Java代码。我想知道是否有一个内置的功能?另外,这个解决方案必须兼容所有主要的Linux发行版& MacOS。有什么想法吗?

java linux macos screen
1个回答
2
投票

使用命令的退出值,使用 which screen如果您在终端机上发出以下命令

which screen 
echo Exitcode:$? 

你得到

/usr/bin/screen
Exitcode:0

也为不存在的命令。

which screeeennn
echo Exitcode:$?

你得到

screeeennn not found
Exitcode:1

在java类中应用同样的逻辑。

        public class ScreenTest {
            public static void main(String args[]) {
                int exitValue;

                try {
                    Process p = Runtime.getRuntime().exec("which screen");
                    exitValue = p.waitFor();
                    if (exitValue > 0) {
                        // not installed mostly value will be 1
                    } else {
                        //screen present value will be zero
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {

                    e.printStackTrace();
                }
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.