kar*_*grz 495 java operating-system
我想确定我的Java程序以编程方式运行的主机的操作系统(例如:我希望能够根据我是在Windows还是Unix平台上加载不同的属性).100%可靠性最安全的方法是什么?
Chr*_*ung 591
您可以使用:
System.getProperty("os.name")
Run Code Online (Sandbox Code Playgroud)
PS您可能会发现此代码很有用:
class ShowProperties {
public static void main(String[] args) {
System.getProperties().list(System.out);
}
}
Run Code Online (Sandbox Code Playgroud)
它所做的就是打印出Java实现提供的所有属性.它将通过属性为您提供有关Java环境的信息.:-)
Lei*_*sen 142
如其他答案所示,System.getProperty提供原始数据.但是,Apache Commons Lang组件为java.lang.System提供了一个包装器,它具有SystemUtils.IS_OS_WINDOWS等方便的属性,就像前面提到的Swingx OS util一样.
Von*_*onC 77
2008年10月:
我建议将其缓存在一个静态变量中:
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
}
Run Code Online (Sandbox Code Playgroud)
这样,每次你要求Os时,你都不会在应用程序的生命周期内多次获取属性.
2016年2月:7年后:
Windows 10存在一个错误(原始答案时不存在).
请参阅" Java的"os.name"for Windows 10? "
Wol*_*ahl 40
上面答案中的一些链接似乎被打破了.我在下面的代码中添加了指向当前源代码的指针,并提供了一种处理检查的方法,将枚举作为答案,以便在评估结果时可以使用switch语句:
OsCheck.OSType ostype=OsCheck.getOperatingSystemType();
switch (ostype) {
case Windows: break;
case MacOS: break;
case Linux: break;
case Other: break;
}
Run Code Online (Sandbox Code Playgroud)
帮助程序类是:
/**
* 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;
}
}
Run Code Online (Sandbox Code Playgroud)
Igo*_*bak 38
以下JavaFX类具有用于确定当前OS的静态方法(isWindows(),isLinux()...):
例:
if (PlatformUtil.isWindows()){
...
}
Run Code Online (Sandbox Code Playgroud)
Mem*_*min 13
假设您有这样的实用程序函数的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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以轻松地从任何类调用类,如下所示(PS因为我们将os变量声明为静态,它只消耗一次时间来识别系统类型,然后它可以在你的应用程序停止之前使用.)
switch (Util.getOS()) {
case WINDOWS:
//do windows stuff
break;
case LINUX:
Run Code Online (Sandbox Code Playgroud)
等等...
小智 12
private static String OS = System.getProperty("os.name").toLowerCase();
public static void detectOS() {
if (isWindows()) {
} else if (isMac()) {
} else if (isUnix()) {
} else {
}
}
private static boolean isWindows() {
return (OS.indexOf("win") >= 0);
}
private static boolean isMac() {
return (OS.indexOf("mac") >= 0);
}
private static boolean isUnix() {
return (OS.indexOf("nux") >= 0);
}
Run Code Online (Sandbox Code Playgroud)
Ale*_*ler 10
如果你对开源项目如何做这样的事情感兴趣,你可以看看处理这个垃圾的Terracotta类(Os.java):
你可以在这里看到一个类似的类来处理JVM版本(Vm.java和VmVersion.java):
小智 9
试试这个,简单易行
System.getProperty("os.name");
System.getProperty("os.version");
System.getProperty("os.arch");
Run Code Online (Sandbox Code Playgroud)
小智 9
取自这个项目https://github.com/RishiGupta12/serial-communication-manager
String osName = System.getProperty("os.name");
String osNameMatch = osName.toLowerCase();
if(osNameMatch.contains("linux")) {
osType = OS_LINUX;
}else if(osNameMatch.contains("windows")) {
osType = OS_WINDOWS;
}else if(osNameMatch.contains("solaris") || osNameMatch.contains("sunos")) {
osType = OS_SOLARIS;
}else if(osNameMatch.contains("mac os") || osNameMatch.contains("macos") || osNameMatch.contains("darwin")) {
osType = OS_MAC_OS_X;
}else {
}
Run Code Online (Sandbox Code Playgroud)
我发现Swingx的OS Utils可以完成这项工作.
下面的代码显示了您可以从System API获得的值,这些是您可以通过此API获得的所有内容.
public class App {
public static void main( String[] args ) {
//Operating system name
System.out.println(System.getProperty("os.name"));
//Operating system version
System.out.println(System.getProperty("os.version"));
//Path separator character used in java.class.path
System.out.println(System.getProperty("path.separator"));
//User working directory
System.out.println(System.getProperty("user.dir"));
//User home directory
System.out.println(System.getProperty("user.home"));
//User account name
System.out.println(System.getProperty("user.name"));
//Operating system architecture
System.out.println(System.getProperty("os.arch"));
//Sequence used by operating system to separate lines in text files
System.out.println(System.getProperty("line.separator"));
System.out.println(System.getProperty("java.version")); //JRE version number
System.out.println(System.getProperty("java.vendor.url")); //JRE vendor URL
System.out.println(System.getProperty("java.vendor")); //JRE vendor name
System.out.println(System.getProperty("java.home")); //Installation directory for Java Runtime Environment (JRE)
System.out.println(System.getProperty("java.class.path"));
System.out.println(System.getProperty("file.separator"));
}
}
Run Code Online (Sandbox Code Playgroud)
回答: -
Windows 7
6.1
;
C:\Users\user\Documents\workspace-eclipse\JavaExample
C:\Users\user
user
amd64
1.7.0_71
http://java.oracle.com/
Oracle Corporation
C:\Program Files\Java\jre7
C:\Users\user\Documents\workspace-Eclipse\JavaExample\target\classes
\
Run Code Online (Sandbox Code Playgroud)
如果您在安全敏感的环境中工作,那么请通读本。
请不要相信通过System#getProperty(String)
子程序获得的属性!实际上,包括os.arch
,os.name
和在内的几乎所有属性os.version
都不是您所期望的只读 - 相反,它们实际上恰恰相反。
首先,任何有足够权限调用System#setProperty(String, String)
子程序的代码都可以随意修改返回的字面量。然而,这不一定是这里的主要问题,因为它可以通过使用所谓的 来解决SecurityManager
,如在此处更详细描述的。
实际的问题是,任何用户在运行时能够编辑这些属性JAR
中的问题(通过-Dos.name=
,-Dos.arch=
等等)。避免篡改应用程序参数的一种可能方法是查询此处RuntimeMXBean
所示的。以下代码片段应提供有关如何实现这一点的一些见解。
RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
List<String> arguments = runtimeMxBean.getInputArguments();
for (String argument : arguments) {
if (argument.startsWith("-Dos.name") {
// System.getProperty("os.name") altered
} else if (argument.startsWith("-Dos.arch") {
// System.getProperty("os.arch") altered
}
}
Run Code Online (Sandbox Code Playgroud)
最佳答案的更短、更清晰(并且热切计算)的版本:
switch(OSType.DETECTED){
...
}
Run Code Online (Sandbox Code Playgroud)
助手枚举:
public enum OSType {
Windows, MacOS, Linux, Other;
public static final OSType DETECTED;
static{
String OS = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH);
if ((OS.contains("mac")) || (OS.contains("darwin"))) {
DETECTED = OSType.MacOS;
} else if (OS.contains("win")) {
DETECTED = OSType.Windows;
} else if (OS.contains("nux")) {
DETECTED = OSType.Linux;
} else {
DETECTED = OSType.Other;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我认为以下内容可以在更少的行中提供更广泛的覆盖范围
import org.apache.commons.exec.OS;
if (OS.isFamilyWindows()){
//load some property
}
else if (OS.isFamilyUnix()){
//load some other property
}
Run Code Online (Sandbox Code Playgroud)
此处有更多详细信息:https : //commons.apache.org/proper/commons-exec/apidocs/org/apache/commons/exec/OS.html
String osName = System.getProperty("os.name");
System.out.println("Operating system " + osName);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
339036 次 |
最近记录: |