Os name linux java

Detect OS Name and Version Java

Java is Platform independent and can run everywhere. Knowing this, it can be worth knowing in what operation system the application is running. To detect the current operation system, you can use the OSInfo class below, which retrieves the information from the system properties and returns an OS enum that holds the name and version of the operating system the application is running on.

Detect Operating System

We can get the current operation system name by calling System.getProperty(«os.name»); . We evaluate this value and appropriately map it to the correct os. Finally we add the version number of the os by calling the System.getProperty(«os.version»); .

package com.memorynotfound.file; import java.io.IOException; import java.util.Locale; public class OSInfo < public enum OS < WINDOWS, UNIX, POSIX_UNIX, MAC, OTHER; private String version; public String getVersion() < return version; >public void setVersion(String version) < this.version = version; >> private static OS os = OS.OTHER; static < try < String osName = System.getProperty("os.name"); if (osName == null) < throw new IOException("os.name not found"); >osName = osName.toLowerCase(Locale.ENGLISH); if (osName.contains("windows")) < os = OS.WINDOWS; >else if (osName.contains("linux") || osName.contains("mpe/ix") || osName.contains("freebsd") || osName.contains("irix") || osName.contains("digital unix") || osName.contains("unix")) < os = OS.UNIX; >else if (osName.contains("mac os")) < os = OS.MAC; >else if (osName.contains("sun os") || osName.contains("sunos") || osName.contains("solaris")) < os = OS.POSIX_UNIX; >else if (osName.contains("hp-ux") || osName.contains("aix")) < os = OS.POSIX_UNIX; >else < os = OS.OTHER; >> catch (Exception ex) < os = OS.OTHER; >finally < os.setVersion(System.getProperty("os.version")); >> public static OS getOs() < return os; >>

Detecting the current os the application is running on.

package com.memorynotfound.file; public class DetectOS < public static void main(String. args)< System.out.println("OS: " + OSInfo.getOs()); System.out.println("OS version: " + OSInfo.getOs().getVersion()); System.out.println("Is mac? " + OSInfo.OS.MAC.equals(OSInfo.getOs())); >>
OS: MAC OS version: 10.10.5 Is mac? true

References

Источник

Читайте также:  How to hide text html

How to detect OS in Java

detect os

This article shows a handy Java class that uses System.getProperty(«os.name») to detect which type of operating system (OS) you are using now.

1. Detect OS (Original Version)

This code can detect Windows , Mac , Unix , and Solaris .

 package com.mkyong.system; public class OSValidator < private static String OS = System.getProperty("os.name").toLowerCase(); public static void main(String[] args) < System.out.println("os.name: " + OS); if (isWindows()) < System.out.println("This is Windows"); >else if (isMac()) < System.out.println("This is Mac"); >else if (isUnix()) < System.out.println("This is Unix or Linux"); >else if (isSolaris()) < System.out.println("This is Solaris"); >else < System.out.println("Your OS is not support!!"); >> public static boolean isWindows() < return (OS.indexOf("win") >= 0); > public static boolean isMac() < return (OS.indexOf("mac") >= 0); > public static boolean isUnix() < return (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0); > public static boolean isSolaris() < return (OS.indexOf("sunos") >= 0); > > 
 os.name: windows 10 This is Windows 

2. Detect OS (Enhanced Version)

Since the operating system will remain the same for the running Java app, we can increase the performance by moving the OS checking to static fields; The static ensures the OS.indexOf checking runs once only.

 package com.mkyong.system; public class OSValidator < private static String OS = System.getProperty("os.name").toLowerCase(); public static boolean IS_WINDOWS = (OS.indexOf("win") >= 0); public static boolean IS_MAC = (OS.indexOf("mac") >= 0); public static boolean IS_UNIX = (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0); public static boolean IS_SOLARIS = (OS.indexOf("sunos") >= 0); public static void main(String[] args) < System.out.println("os.name: " + OS); if (IS_WINDOWS) < System.out.println("This is Windows"); >else if (IS_MAC) < System.out.println("This is Mac"); >else if (IS_UNIX) < System.out.println("This is Unix or Linux"); >else if (IS_SOLARIS) < System.out.println("This is Solaris"); >else < System.out.println("Your OS is not support!!"); >> > 

Источник

Java — detect operating system name (Windows, Linux, macOS)

Payne

In this short article, we would like to show how to detect used operating system in Java application.

String osName = System.getProperty("os.name"); // Windows 10 // Linux // Mac OS X // etc.
// import org.apache.commons.lang3.SystemUtils; boolean isWindows = SystemUtils.IS_OS_WINDOWS; boolean isLinux = SystemUtils.IS_OS_LINUX; boolean isMac = SystemUtils.IS_OS_MAC;

Where: SystemUtils should be attached as dependecy (Maven dependency is avaialble here).

Operating system name detection

In this section, you can find information what os.name property should return on specific operating system.

Operating system type detection

Knowledge about operating system name may be requred when some operating system uses specific configuration or resources. In this section we would like to show different aproaches to detect operating system type.

1. Apache Commons library

Using this approach it is required to attach commons-lang3 library provided by Apache Software Foundation.

Usage example (it was run under Windows 10):

import org.apache.commons.lang3.SystemUtils; public class Program < public static void main(String[] args) < // Most popular usage: System.out.println(SystemUtils.IS_OS_WINDOWS); // true System.out.println(SystemUtils.IS_OS_LINUX); // false System.out.println(SystemUtils.IS_OS_MAC); // false // Less popular usage: System.out.println(SystemUtils.IS_OS_SOLARIS); // false System.out.println(SystemUtils.IS_OS_SUN_OS); // false >>

Hint:

Using commons-lang3 library we are able to detect more precised OS version, e.g. SystemUtils.IS_OS_WINDOWS_8 , SystemUtils.IS_OS_WINDOWS_10 , etc.

true false false false false
  org.apache.commons commons-lang3 3.12.0 

2. Custom solution

In this section, you can find simple custom solution that lets to detect Windows , Linux or Mac operating system. For more precised detection it is recommended to use commons-lang3 library.

Usage example (it was run under Windows 10):

OS code: Windows OS name: Windows 10 is Windows: true is Linux: false is Mac: false

Example SystemUtils.java file:

public class SystemUtils < /** Returns OS name, e.g. Windows 10, Linux, Mac OS X, etc. */ public static final String OS_NAME = getOSName(); private static final String OS_TEXT = OS_NAME.toLowerCase(); public static final boolean IS_OS_WINDOWS = OS_TEXT.startsWith("windows"); public static final boolean IS_OS_LINUX = OS_TEXT.startsWith("linux"); public static final boolean IS_OS_MAC = OS_TEXT.startsWith("mac"); /** Returns OS code, e.g. Windows, Linux, Mac, Unknown */ public static final String OS_CODE = getOSCode(); private SystemUtils() < // Nothing here . >private static String getOSName() < return System.getProperty("os.name"); >private static String getOSCode() < if (IS_OS_WINDOWS) < return "Windows"; >if (IS_OS_LINUX) < return "Linux"; >if (IS_OS_MAC) < return "Mac"; >return "Unknown"; > >

References

Alternative titles

  1. Java — check operating system name (Windows, Linux or Mac OS)
  2. Java — detect OS name
  3. Java — check OS name (Windows, Linux or Mac OS)
  4. Java — get operating system name
  5. Java — get operating system name (Windows, Linux or Mac OS)
  6. Java — get OS name
  7. Java — get OS name (Windows, Linux or Mac OS)
  8. Java — detect OS type
  9. Java — get OS type
  10. Java — check OS type
  11. Java — determine operating system

Источник

Java Program to get Operating System name and version

Use the System.getProperty() method in Java to get the Operating System name and version.

String getProperty(String key)

Above, the key is the name of the system property. Since, we want the OS name and version, therefore we will add the key as −

Operating System name - os.name Operating System version - os.version

The following is an example −

Example

Output

OS Name: Linux OS Version: 3.10.0-862.9.1.el7.x86_64

Samual Sam

Learning faster. Every day.

  • Related Articles
  • Java Program to determine the name and version of the operating system
  • Haskell Program to determine the name and version of the operating system
  • Display the Operating System name in Java
  • Get Operating System temporary directory / folder in Java
  • How to get android application version name?
  • Difference Between Network Operating System and Distributed Operating System
  • Java Program to get full day name
  • Java program to Get IP address of the system
  • Java Program to get name of parent directory
  • Difference between System Software and Operating System
  • How to get Java and OS version, vendor details in JShell in Java 9?
  • Operating System Design and Implementation
  • Difference between a Centralised Version Control System (CVCS) and a Distributed Version Control System (DVCS)
  • How to get the application name and version information of a browser in JavaScript?
  • Operating System Structure

Источник

Os name linux java

There are many ways of getting the linux distribution in Java. Here you can find some examples that may suit your needs.

Getting the version from the System properties

package com.exec; import java.util.Properties; public class ExecRealease extends Thread < public static void main(String[] args) < Properties props = System.getProperties(); System.out.println(props.getProperty("os.name")); System.out.println(props.getProperty("os.version")); >>

Using Runtime.getRuntime().exec(. ) to get it with a system command.

This example prints all the release related files using cat under the folder etc

package com.exec; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class ExecRealease extends Thread < public static void main(String[] args) < String[] cmd = ; try < Process p = Runtime.getRuntime().exec(cmd); BufferedReader bri = new BufferedReader(new InputStreamReader( p.getInputStream())); String line = ""; while ((line = bri.readLine()) != null) < System.out.println(line); >bri.close(); > catch (IOException e) < e.printStackTrace(); >> >

*change cat /etc/*-release for uname -a if you just need the distribution

Using Just Java to read the version files

Uses Java to read all the version related files including the version file included under /proc

import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FilenameFilter; import java.util.Arrays; public class ExecRealease extends Thread < public static void main(String[] args) < //lists all the files ending with -release in the etc folder File dir = new File("/etc/"); File fileList[] = new File[0]; if(dir.exists())< fileList = dir.listFiles(new FilenameFilter() < public boolean accept(File dir, String filename) < return filename.endsWith("-release"); >>); > //looks for the version file (not all linux distros) File fileVersion = new File("/proc/version"); if(fileVersion.exists()) < fileList = Arrays.copyOf(fileList,fileList.length+1); fileList[fileList.length-1] = fileVersion; >//prints all the version-related files for (File f : fileList) < try < BufferedReader myReader = new BufferedReader(new FileReader(f)); String strLine = null; while ((strLine = myReader.readLine()) != null) < System.out.println(strLine); >myReader.close(); > catch (Exception e) < System.err.println("Error: " + e.getMessage()); >> > >

Ads

Latests Posts

Источник

Оцените статью