How to get the operating system name in Java

2 Answers

0 votes
public class MyClass {
    public static void main(String args[]) {
        String osName = System.getProperty("os.name");
        
        System.out.println(osName);
    }
}
       
       
       
       
/*
run:
       
Linux
       
*/

 



answered Oct 7, 2023 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        String osName = System.getProperty("os.name");
        
        if (osName.startsWith("Windows")) {
            System.out.println("Windows");
        } else if (osName.startsWith("Mac")) {
            System.out.println("Mac");
        } else if (osName.startsWith("Linux")) {
            System.out.println("Linux");
        }
    }
}
       
       
       
       
/*
run:
       
Linux
       
*/

 



answered Oct 7, 2023 by avibootz

Related questions

1 answer 269 views
2 answers 277 views
277 views asked Mar 20, 2023 by avibootz
1 answer 223 views
2 answers 378 views
1 answer 346 views
1 answer 312 views
...