【问题标题】:java: detect if 32 or 64 bit applications can be executedjava:检测是否可以执行 32 位或 64 位应用程序
【发布时间】: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


【解决方案1】:

我当前的解决方案如下所示:

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

public static int getApplicationArchitecture(){
    if(isWindows()){
        String arch = System.getenv("PROCESSOR_ARCHITECTURE");
        String wow64Arch = System.getenv("PROCESSOR_ARCHITEW6432");

        if(arch!=null && arch.endsWith("64")){
            return 64;
        }
        if(wow64Arch != null && wow64Arch.endsWith("64")){
            return 64;
        }
    }
    else if(isMac() || isUnix()){
        try{
            Process p = Runtime.getRuntime().exec("uname -m");
            InputStream is = p.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line = br.readLine();
            if(line.indexOf("64")!=-1){
                return 64;
            }
        }catch(Exception e){}
    }

    // on unkown OS
    if(System.getProperty("os.arch").equals("x86")){
        return 32;
    }
    return 64;
}

最后我会检查 64 位操作系统的具体情况,如果不能确定,请检查 os.arch 属性 - 这有点猜测位数

也许有人可以评论或改进

【讨论】:

    【解决方案2】:

    只需从依赖于操作系统的 JAVA 执行 shell 命令并检查它的输出。 例如: * Windows:wmic os 获取 osarchitecture * Linux 喜欢:uname -m

    【讨论】:

    • 谢谢,我添加了你的 uname 解决方案,但是由于 wmic 输出解析起来有点复杂,而且我没有 windows,而且似乎有一个 getenv 解决方案会在 windows 上粘贴哪个 Environment 属性
    猜你喜欢
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 2013-10-04
    • 2011-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多