从 Java Applet 获取 Windows Service Pack 版本?

发布于 2024-07-26 14:11:12 字数 511 浏览 3 评论 0 原文

我正在写一个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) 上运行”的内容,这很棒,但我也需要能够获取服务包版本。

有人知道如何从 Java 小程序中获取 Windows 计算机的底层服务包版本吗? (无需抛出 AccessControlException,或者理想情况下无需对小程序进行自签名)。

提前谢谢了。

I am writing a Java Applet. When run on Windows, I need to be able to get the clients OS version, e.g. Windows XP SP3 or Windows 2000 SP4.

I can currently use the following:

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

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

And it will output something like "Running on Windows 2000 (5.0)" which is great but I need to be able to get the service pack version too.

Anybody know how I can get the underlying service pack version of a Windows machine from within a Java applet? (Without throwing an AccessControlException, or ideally without having to self sign the applet).

Many thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

Bonjour°[大白 2024-08-02 14:13:29

简而言之,您无法在不引发 AccessControlException 的情况下获取 sun.os.patch.level 属性的值。

小程序有安全限制。

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

Put simply you can't get the value of the sun.os.patch.level property without throwing an AccessControlException.

Applets have security restrictions imposed on.

The only thing you can do is to read on signed applets and policy files for applets

凝望流年 2024-08-02 14:13:07

要获取操作系统版本(主要、次要、构建、服务包),您可以使用 JNA 此处的文档 以及任意数量的 Windows 库(version.dll、kernel32.dll、user32.dll 等)。 我的项目基于 Daniel Doubrovkine 的项目操作系统和硬件信息。 他的项目还将查询我不需要的硬件。 好处是它还允许您读取/写入注册表。

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;
    }
}

To get the OS version (major, minor, build, service pack) you can use JNA Documentation Here and any number of the windows libraries (version.dll, kernel32.dll, user32.dll, etc). I based my project on Daniel Doubrovkine's project Operating System and Hardware Information. His project will also query the hardware, which I didn't need. The nice thing is it will also allow you to read/write to the registry.

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;
    }
}
眼中杀气 2024-08-02 14:12:45

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

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

i think you can get it using the 'sun.os.patch.level' property:

String os_sp = System.getProperty( "sun.os.patch.level" );
失而复得 2024-08-02 14:12:25

您可以对您的java小程序进行自签名:(

窃取自:http://www.captain.at/编程/java/

制作证书:

keytool -export -alias yourkey -file yourcert.crt 
  

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

只需创建一个 *.bat 文件,其中包含以下内容:

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

批处理文件编译小程序,
制作一个 jar 档案并签署
jar 文件。

显示小程序的 HTML 代码:

 
  
    

高度=“500”>

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

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

You can self-sign your java applet:

(stolen from: http://www.captain.at/programming/java/)

Make the certificate:

keytool -export -alias yourkey -file yourcert.crt

Now we have to sign the applet:

Just make a *.bat file including this:

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

The batch-file compiles the applet,
makes a jar-archive and signs the
jar-file.

The HTML-code to display the applet:

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

height="500">

Now we are done! The applet is signed
and if the user accepts the
certificate, the applet is allowed to
access local files. If the user
doesn't agree, you get a
java.security.AccessControlException.

So, as long as you don't mind about this little dialog box...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文