我想确定我的Java程序以编程方式运行的主机的操作系统(例如:我希望能够根据我是在Windows还是Unix平台上加载不同的属性)。100%可靠的最安全的方法是什么?
当前回答
你想要实现的一个小例子可能是一个类似于下面的类:
import java.util.Locale;
public class OperatingSystem
{
private static String OS = System.getProperty("os.name", "unknown").toLowerCase(Locale.ROOT);
public static boolean isWindows()
{
return OS.contains("win");
}
public static boolean isMac()
{
return OS.contains("mac");
}
public static boolean isUnix()
{
return OS.contains("nux");
}
}
这个特殊的实现非常可靠,应该是普遍适用的。只需复制粘贴到你选择的类。
其他回答
博士TL;
访问操作系统使用:System.getProperty(" OS .name")。
但是等等! !
为什么不创建一个实用工具类,使其可重用!而且在多次通话中可能会更快。干净,清晰,快点!
为这样的实用函数创建一个Util类。然后为每种操作系统类型创建公共枚举。
public class Util {
public enum OS {
WINDOWS, LINUX, MAC, SOLARIS
};// Operating systems.
private static OS os = null;
public static OS getOS() {
if (os == null) {
String operSys = System.getProperty("os.name").toLowerCase();
if (operSys.contains("win")) {
os = OS.WINDOWS;
} else if (operSys.contains("nix") || operSys.contains("nux")
|| operSys.contains("aix")) {
os = OS.LINUX;
} else if (operSys.contains("mac")) {
os = OS.MAC;
} else if (operSys.contains("sunos")) {
os = OS.SOLARIS;
}
}
return os;
}
}
现在,您可以轻松地从任何类中调用类,如下所示由于我们将os变量声明为静态,它只会花费一次时间来识别系统类型,然后它可以一直使用到应用程序停止。)
switch (Util.getOS()) {
case WINDOWS:
//do windows stuff
break;
case LINUX:
就是这样!
获取操作系统名称,只需使用:
Platform.getOS()
假设你想看看平台是否是linux:
if (Platform.getOS().equals(Platform.OS_LINUX)) {
}
类似地,Platform类为其他操作系统名称定义了常量。平台类是org.eclipse.core.runtime包的一部分。
因为谷歌点“kotlin os名称”到这个页面,这里是@Memin的答案的kotlin版本:
private var _osType: OsTypes? = null
val osType: OsTypes
get() {
if (_osType == null) {
_osType = with(System.getProperty("os.name").lowercase(Locale.getDefault())) {
if (contains("win"))
OsTypes.WINDOWS
else if (listOf("nix", "nux", "aix").any { contains(it) })
OsTypes.LINUX
else if (contains("mac"))
OsTypes.MAC
else if (contains("sunos"))
OsTypes.SOLARIS
else
OsTypes.OTHER
}
}
return _osType!!
}
enum class OsTypes {
WINDOWS, LINUX, MAC, SOLARIS, OTHER
}
上面答案中的一些链接似乎被打破了。我在下面的代码中添加了指向当前源代码的指针,并提供了一种方法来处理以enum作为答案的检查,以便在计算结果时使用switch语句:
OsCheck.OSType ostype=OsCheck.getOperatingSystemType();
switch (ostype) {
case Windows: break;
case MacOS: break;
case Linux: break;
case Other: break;
}
helper类是:
/**
* helper class to check the operating system this Java VM runs in
*
* please keep the notes below as a pseudo-license
*
* http://stackoverflow.com/questions/228477/how-do-i-programmatically-determine-operating-system-in-java
* compare to http://svn.terracotta.org/svn/tc/dso/tags/2.6.4/code/base/common/src/com/tc/util/runtime/Os.java
* http://www.docjar.com/html/api/org/apache/commons/lang/SystemUtils.java.html
*/
import java.util.Locale;
public static final class OsCheck {
/**
* types of Operating Systems
*/
public enum OSType {
Windows, MacOS, Linux, Other
};
// cached result of OS detection
protected static OSType detectedOS;
/**
* detect the operating system from the os.name System property and cache
* the result
*
* @returns - the operating system detected
*/
public static OSType getOperatingSystemType() {
if (detectedOS == null) {
String OS = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH);
if ((OS.indexOf("mac") >= 0) || (OS.indexOf("darwin") >= 0)) {
detectedOS = OSType.MacOS;
} else if (OS.indexOf("win") >= 0) {
detectedOS = OSType.Windows;
} else if (OS.indexOf("nux") >= 0) {
detectedOS = OSType.Linux;
} else {
detectedOS = OSType.Other;
}
}
return detectedOS;
}
}
String osName = System.getProperty("os.name");
System.out.println("Operating system " + osName);
推荐文章
- 假唤醒在Java中真的发生了吗?
- 如何按字母顺序排序列表?
- 如何在java中格式化持续时间?(如格式H:MM:SS)
- urlencoder .encode(字符串)已弃用,我应该使用什么代替?
- javax.transaction.Transactional vs . org.springframework.transaction.annotation.Transactional
- Java 8接口方法中不允许“同步”的原因是什么?
- 如何找到Java堆大小和内存使用(Linux)?
- 使用Enum实现单例(Java)
- RabbitMQ与通道和连接之间的关系
- buildSessionFactory()配置方法在Hibernate中已弃用?
- Spring MVC -如何获得所有的请求参数在一个地图在Spring控制器?
- 如何在Java中按两个字段排序?
- 文件之间的差异。路径中的分隔符和斜杠
- 在方法参数中使用NotNull注释
- Spring MVC中处理可选参数的@RequestParam