从Java Applet获取Windows Service Pack版本?

时间:2009-06-30 00:08:50

标签: windows applet java

我正在编写一个Java Applet。在Windows上运行时,我需要能够获得客户端操作系统版本,例如Windows XP SP3或Windows 2000 SP4。

我目前可以使用以下内容:

String os_name    = System.getProperty( "os.name" );
String os_version = System.getProperty( "os.version" );

System.out.println( "Running on " + os_name + "(" + os_version + ")" );

它会输出类似“在Windows 2000(5.0)上运行”的内容,这很棒,但我需要能够获得Service Pack版本。

有人知道如何从Java applet中获取Windows机器的底层Service Pack版本吗? (不抛出AccessControlException,或者理想情况下无需对applet进行自签名)。

非常感谢提前。

4 个答案:

答案 0 :(得分:4)

您可以自行签署您的Java小程序:

(被盗:http://www.captain.at/programming/java/

  

制作证书:

keytool -export -alias yourkey -file yourcert.crt
     

现在我们必须签署小程序:

     

只需制作一个* .bat文件,包括:

javac yourapplet.java
jar cvf yourapplet.jar yourapplet.class
jarsigner yourapplet.jar yourkey
     

批处理文件编译applet,   制作一个jar存档并签名   罐子文件。

     

显示小程序的HTML代码:

<applet code="yourapplet.class" archive="yourapplet.jar" width="600"
     

高度= “500” &GT;       

     

现在我们完成了!小程序已签名   如果用户接受了   证书,小程序是允许的   访问本地文件。如果是用户   不同意,你得到了   java.security.AccessControlException

所以,只要你不介意这个小对话框......

答案 1 :(得分:3)

我认为你可以使用'sun.os.patch.level'属性来获取它:

String os_sp = System.getProperty( "sun.os.patch.level" );

答案 2 :(得分:1)

要获取操作系统版本(主要版本,次要版本,构建版本,Service Pack),您可以使用JNA Documentation Here和任意数量的Windows库(version.dll,kernel32.dll,user32.dll等)。我的项目基于Daniel Doubrovkine的项目Operating System and Hardware Information。他的项目也将查询我不需要的硬件。好消息是它还允许您读/写注册表。

import com.sun.jna.Native;
import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.Win32Exception;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.platform.win32.WinNT.OSVERSIONINFOEX;
import com.sun.jna.platform.win32.WinReg;
import com.sun.jna.platform.win32.WinUser;

/**
 *
 * @author geverding
 */
public class OSVersionInfo {

    private OSVERSIONINFOEX versionInfo;
    private static OSVersionInfo instance = new OSVersionInfo();

    public static OSVersionInfo Istance() {
        return instance;
    }

    public OSVersionInfo() {
        versionInfo = new OSVERSIONINFOEX();
        if (!Kernel32.INSTANCE.GetVersionEx(versionInfo))
        {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
    }

    public int getMajor() {
        return versionInfo.dwMajorVersion.intValue();
    }

    public int getMinor() {
        return versionInfo.dwMinorVersion.intValue();
    }

    public int getBuild() {
        return versionInfo.dwBuildNumber.intValue();

    }

    public String getServicePack() {
        return Native.toString(versionInfo.szCSDVersion);
    }

    @Override
    public String toString() {
        String version = null;

        if (versionInfo.dwPlatformId.intValue() == WinNT.VER_PLATFORM_WIN32_NT)
        {
            // 7
            if (versionInfo.dwMajorVersion.intValue() == 6
                    && versionInfo.dwMinorVersion.intValue() == 1
                    && versionInfo.wProductType == WinNT.VER_NT_WORKSTATION)
            {
                version = "7";
            } // Server 2008 R2
            else if (versionInfo.dwMajorVersion.intValue() == 6
                    && versionInfo.dwMinorVersion.intValue() == 1
                    && versionInfo.wProductType != WinNT.VER_NT_WORKSTATION)
            {
                version = "Server 2008 R2";
            } // Server 2008
            else if (versionInfo.dwMajorVersion.intValue() == 6
                    && versionInfo.dwMinorVersion.intValue() == 0
                    && versionInfo.wProductType != WinNT.VER_NT_WORKSTATION)
            {
                version = "Server 2008";
            } // Vista
            else if (versionInfo.dwMajorVersion.intValue() == 6
                    && versionInfo.dwMinorVersion.intValue() == 0
                    && versionInfo.wProductType == WinNT.VER_NT_WORKSTATION)
            {
                version = "Vista";
            } // Server 2003
            else if (versionInfo.dwMajorVersion.intValue() == 5
                    && versionInfo.dwMinorVersion.intValue() == 2
                    && versionInfo.wProductType != WinNT.VER_NT_WORKSTATION
                    && User32.INSTANCE.GetSystemMetrics(WinUser.SM_SERVERR2) != 0)
            {
                version = "Server 2003";
            } // Server 2003 R2
            else if (versionInfo.dwMajorVersion.intValue() == 5
                    && versionInfo.dwMinorVersion.intValue() == 2
                    && versionInfo.wProductType != WinNT.VER_NT_WORKSTATION
                    && User32.INSTANCE.GetSystemMetrics(WinUser.SM_SERVERR2) == 0)
            {
                version = "Server 2003 R2";
            } // XP 64 bit
            else if (versionInfo.dwMajorVersion.intValue() == 5
                    && versionInfo.dwMinorVersion.intValue() == 2
                    && versionInfo.wProductType == WinNT.VER_NT_WORKSTATION)
            {
                version = "XP";
            } // XP 32 bit
            else if (versionInfo.dwMajorVersion.intValue() == 5
                    && versionInfo.dwMinorVersion.intValue() == 1)
            {
                version = "XP";
            } // 2000
            else if (versionInfo.dwMajorVersion.intValue() == 5
                    && versionInfo.dwMinorVersion.intValue() == 0)
            {
                version = "2000";
            } // Windows NT
            else if (versionInfo.dwMajorVersion.intValue() == 4)
            {
                version = "NT 4";

                if ("Service Pack 6".equals(Native.toString(versionInfo.szCSDVersion)))
                {
                    if (Advapi32Util.registryKeyExists(WinReg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\WindowsNT\\CurrentVersion\\Hotfix\\Q246009"))
                    {

                        return "NT4 SP6a";
                    }
                }

            } else
            {
                throw new RuntimeException("Unsupported Windows NT version: "
                        + versionInfo.toString());
            }

            if (versionInfo.wServicePackMajor.intValue() > 0)
            {
                version = version + " SP" + versionInfo.wServicePackMajor.intValue();
            }

        } else if (versionInfo.dwPlatformId.intValue() == WinNT.VER_PLATFORM_WIN32_WINDOWS)
        {
            if (versionInfo.dwMajorVersion.intValue() == 4
                    && versionInfo.dwMinorVersion.intValue() == 90)
            {
                version = "ME";
            } else if (versionInfo.dwMajorVersion.intValue() == 4
                    && versionInfo.dwMinorVersion.intValue() == 10)
            {
                if (versionInfo.szCSDVersion[1] == 'A')
                {
                    version = "98 SE";
                } else
                {
                    version = "98";
                }
            } else if (versionInfo.dwMajorVersion.intValue() == 4
                    && versionInfo.dwMinorVersion.intValue() == 0)
            {
                if (versionInfo.szCSDVersion[1] == 'C' || versionInfo.szCSDVersion[1] == 'B')
                {
                    version = "95 OSR2";
                } else
                {
                    version = "95";
                }
            } else
            {
                throw new RuntimeException("Unsupported Windows 9x version: "
                        + versionInfo.toString());
            }
        } else
        {
            throw new RuntimeException("Unsupported Windows platform: "
                    + versionInfo.toString());
        }

        return version;
    }
}

答案 3 :(得分:0)

简单地说,如果不抛出AccessControlException就无法获取sun.os.patch.level属性的值。

小程序对安全施加了限制。

您唯一能做的就是阅读已签名的applet和applet的策略文件