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 191 views
2 answers 152 views
152 views asked Mar 20, 2023 by avibootz
1 answer 125 views
2 answers 248 views
1 answer 257 views
1 answer 209 views
...