Skip to content

Instantly share code, notes, and snippets.

@monkstone
Created June 3, 2018 07:15
Show Gist options
  • Save monkstone/b6581f3709eefb17cc53f68df40ab3cf to your computer and use it in GitHub Desktop.
Save monkstone/b6581f3709eefb17cc53f68df40ab3cf to your computer and use it in GitHub Desktop.
Java Test For Operating Systems
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class TestOs {
private static boolean isWindows = false;
private static boolean isLinux = false;
private static boolean isHpUnix = false;
private static boolean isPiUnix = false;
private static boolean isSolaris = false;
private static boolean isSunOS = false;
private static boolean archDataModel32 = false;
private static boolean archDataModel64 = false;
static {
final String os = System.getProperty("os.name").toLowerCase();
if (os.contains("windows")) {
isWindows = true;
}
if (os.contains("linux")) {
isLinux = true;
}
if (os.contains("hp-ux")) {
isHpUnix = true;
}
if (os.contains("hpux")) {
isHpUnix = true;
}
if (os.contains("solaris")) {
isSolaris = true;
}
if (os.contains("sunos")) {
isSunOS = true;
}
if (System.getProperty("sun.arch.data.model").equals("32")) {
archDataModel32 = true;
}
if (System.getProperty("sun.arch.data.model").equals("64")) {
archDataModel64 = true;
}
if (isLinux) {
final File file = new File("/etc", "os-release");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String string;
while ((string = br.readLine()) != null) {
if (string.toLowerCase().contains("raspbian")) {
if (string.toLowerCase().contains("name")) {
isPiUnix = true;
break;
}
}
}
} catch (final IOException e) {
}
}
}
public static void main(String[] args) {
System.out.println(isPiUnix);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment