I have a large Java program that also executes other programs, and some of them runs on .NET 6. That's why, when the program loads, I want to check if .NET is installed so I can show the user an error message prompting them to download and install it.
What I'm trying to do right now is to run the dotnet --list-runtimes
command, following these Microsoft recommendations.
private boolean isNET6Installed() {
// Checking if .NET is installed using the dotnet command.
try {
Process process = Runtime.getRuntime().exec("cmd /c dotnet --list-runtimes");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String dotnetRuntime = "";
while ((dotnetRuntime = reader.readLine()) != null) {
String dotnetMajorVersion = dotnetRuntime.split("\\ ")[1].split("\\.")[0];
if(Integer.parseInt(dotnetMajorVersion) >= 6){
return true;
}
}
}catch(IOException e) {
Debug.debug(e);
}
return false;
}
Running the same command from my terminal I get the following output:
However, in my program, the process (or reader.readLine()
) always returns null
. Is there a better way of doing this?