I'd need a way to check if the Linux Screen package is installed but through Java code. I would like to know if there's a built-in function for that? Also, the solution must be compatible with all major Linux distros & MacOS. Any ideas?
Asked
Active
Viewed 300 times
0
-
Have you thought of running [`which`/`whereis`/`locate`](https://askubuntu.com/questions/799776/what-is-the-difference-between-locate-whereis-which) in a [Process](https://stackoverflow.com/questions/3774432/starting-a-process-in-java)? – mbx Jun 11 '20 at 12:27
1 Answers
2
use the exit value of the command using which screen
in terminal if you issue the command
which screen
echo Exitcode:$?
you get
/usr/bin/screen
Exitcode:0
also for a non existent command,
which screeeennn
echo Exitcode:$?
you get
screeeennn not found
Exitcode:1
Applying the same logic in the java class
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();
}
}
}

Noushad
- 384
- 3
- 11
-
-
1@Noushad Exitcode is 0 in both cases. It should be Exitcode:1 in the second instance – Dmitry Pisklov Jun 11 '20 at 12:43
-