Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to detect the operating system in Java

2 Answers

0 votes
public class MyClass
{
    public enum OS {
        WINDOWS, LINUX, MAC, SOLARIS
    };
  
    public static OS GetOperatingSystem() {
        String os = System.getProperty("os.name").toLowerCase();
  
        if (os.contains("win")) {
            return OS.WINDOWS;
        }
  
        else if (os.contains("nix") || os.contains("nux") || os.contains("aix")) {
            return OS.LINUX;
        }
  
        else if (os.contains("mac")) {
            return OS.MAC;
        }
  
        else if (os.contains("sunos")) {
            return OS.SOLARIS;
        }
  
        return null;
    }
  
    public static void main(String[] args)
    {
        switch (GetOperatingSystem()) {
            case WINDOWS:
                System.out.println("Windows");
                break;
            case LINUX:
                System.out.println("Linux");
                break;
            case MAC:
                System.out.println("Mac");
                break;
            case SOLARIS:
                System.out.println("Solaris");
                break;
            default:
                System.out.println("Unknown");
        }
    }
}
 
 
 
 
/*
run:
 
Linux
 
*/

 



answered Mar 20, 2023 by avibootz
0 votes
public class MyClass
{
    public static String GetOperatingSystem() {
        String os = System.getProperty("os.name").toLowerCase();
 
        if (os.contains("win")) {
            return "Windows";
        }
 
        else if (os.contains("aix") || os.contains("nux") || os.contains("nix")) {
            return "Linux/Unix";
        }
 
        else if (os.contains("mac")) {
            return "Mac";
        }
 
        else if (os.contains("sunos")) {
            return "Solaris";
        }
        
        return "Unknown";
    }
 
    public static void main(String[] args)
    {
        System.out.println(GetOperatingSystem());
    }
}





/*
run:

Linux/Unix

*/

 



answered Mar 20, 2023 by avibootz

Related questions

1 answer 120 views
1 answer 132 views
132 views asked Apr 12, 2022 by avibootz
2 answers 124 views
1 answer 104 views
1 answer 156 views
2 answers 205 views
...