0

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:

cmd /k dotnet --list-runtimes

However, in my program, the process (or reader.readLine()) always returns null. Is there a better way of doing this?

Alexandre Paiva
  • 304
  • 3
  • 13
  • Your code works for me. Try replacing `getInputStream` with `getErrorStream`, and see whether there is some problem running `dotnet`. Perhaps for some reason when you run your java program the PATH is different and it doesn't see `dotnet`. – tgdavies Jun 29 '23 at 22:18
  • 1
    Do yourself a favour and use `new ProcessBuilder(array).inheritIO();` where each element in the array is one token of the four in your command. Your output/errors will appear after you call `start()` – g00se Jun 29 '23 at 23:02

0 Answers0