Is there a way to tell in Java if the program is running on a Windows or Linux machine? I have a .jar file that I want to run on both.
您可以使用以下方式获取操作系统类型
System.getProperty("os.name")
here you go,
How do I programmatically determine operating system in Java?
VonC's answer
public static final class OsUtils
{
private static String OS = null;
public static String getOsName()
{
if(OS == null) { OS = System.getProperty("os.name"); }
return OS;
}
public static boolean isWindows()
{
return getOsName().startsWith("Windows");
}
public static boolean isUnix() // and so on
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.