【发布时间】:2023-04-02 15:24:01
【问题描述】:
我有一个可用于 Windows、*nix 和 mac 的 32 位和 64 位可执行文件。 所以,我的java应用程序需要检测可以运行的程序的操作系统和位架构,才能知道从哪个开始。
我已经搜索了一种解决方案来检测操作系统、jvm 等的位数,但我真的没有找到唯一一个可以在任何操作系统上返回 32 或 64 的函数。 我找到的答案也像“我认为 os.arch 如果可能在某种 Windows 盒子上应该返回这个” - wtf?
问题: 我如何知道使用 Runtime.getRuntime().exec() 肯定会执行哪个应用程序架构?
所以我要求这样的功能:
public int getApplicationArchitecture(){
if(osCanRun32bit())
return 32;
else
return 64;
}
可以假设使用如下类(复制自mkyong.com):
public class OSValidator {
private static String OS = System.getProperty("os.name").toLowerCase();
public static void main(String[] args) {
System.out.println(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);
}
}
感谢任何提示
【问题讨论】:
-
如果安装了 32 位应用程序,它肯定会在两者上“执行”。
-
@EJP 是假的,在某些情况下,当您无法在 64 位平台上运行 32 位应用程序时,例如,当您在 64 位 ubuntu 上设置 32 位 Skype 时,您运行它需要很多额外的库。
-
System.getProperty("os.arch").contains("64")?64:32("amd64"/"i586")有什么问题 -
@DmitryGinzburg 你说的 amd64/i586 是什么意思?这个值是唯一不匹配的 contains("64") 并且是 64?那么您的解决方案将适用于所有主要系统吗?
标签: java architecture