Java 和 Hyper-V

发布于 2024-11-07 20:47:39 字数 441 浏览 1 评论 0原文

我正在尝试学习一些有关 JAVA 的知识,最好的方法是创建一些我实际使用的东西并知道它的目的是什么。 我正在尝试与 HyperV(WMI 库)进行通信。

例如我发现以下内容,我的问题是:如何使用它?我正在使用 Netbeans 创建 GUI。

http://www.paulneve.com/wlab/javadoc /org/paulneve/wlab/virtualization/VirtualizationAccessHyperVImpl.html

另外,如何将 jInterop 加载到我的项目中以便我可以使用它?

谢谢。

I am trying to learn something about JAVA and the best way to do this is creating something I would actually use and know what the purpose of it is.
I am trying to communicate with HyperV (WMI Library).

For example I found the following, my question is: how to use it? I am using Netbeans to create the GUI.

http://www.paulneve.com/wlab/javadoc/org/paulneve/wlab/virtualisation/VirtualisationAccessHyperVImpl.html

Also, how to load jInterop into my project so I can use it?

Thank you.

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

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

发布评论

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

评论(1

风渺 2024-11-14 20:47:39

您可以使用 JInterop 来管理 HyperV 的所有操作。
此处下载 JInterop。
将所有 JInterop jar 文件添加到您的项目构建路径。
以下是获取 Hyper 服务器的所有虚拟机的示例:

public class ManageHyperV {
    static final int RETURN_IMMEDIATE = 0x10;
    static final int FORWARD_ONLY = 0x20;     
    private static final int STOP = 0;
    private static final int START = 1;
    static IJIDispatch msvmServices = null;


    private static IJIDispatch createCOMServer(String namespace) { //root//virtualization
        JIComServer comServer;
        try {           
            JISystem.getLogger().setLevel(Level.WARNING);
            JISystem.setAutoRegisteration(true);

            JISession session = JISession.createSession(domainName,userName,password);
            session.useSessionSecurity(false);
            comServer = new JIComServer(valueOf("WbemScripting.SWbemLocator"),hostIP,session);

            IJIDispatch wbemLocator = (IJIDispatch) narrowObject(comServer.createInstance().queryInterface(IID));
            //parameters to connect to WbemScripting.SWbemLocator
            Object[] params = new Object[] {
                    new JIString(hostIP),//strServer
                    new JIString(namespace),//strNamespace
                    //                  new JIString("ROOT\\CIMV2"),
                    JIVariant.OPTIONAL_PARAM(),//strUser 
                    JIVariant.OPTIONAL_PARAM(),//strPassword 
                    JIVariant.OPTIONAL_PARAM(),//strLocale 
                    JIVariant.OPTIONAL_PARAM(),//strAuthority
                    new Integer(0),//iSecurityFlags 
                    JIVariant.OPTIONAL_PARAM()//objwbemNamedValueSet
            };
            JIVariant results[] = wbemLocator.callMethodA("ConnectServer", params);
            IJIDispatch wbemServices = (IJIDispatch) narrowObject(results[0].getObjectAsComObject());
            return wbemServices;
        } catch (JIException jie) {
            System.out.println(jie.getMessage());
            jie.printStackTrace();
        } catch (JIRuntimeException jire) {
            jire.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static void getVMList() throws JIException {

            String temp = "select * from Msvm_ComputerSystem";          
            String[] arrQuery = new String[]{temp};

            for (int k=0;k<arrQuery.length;k++) {

                Object[] params = new Object[] {
                        new JIString(arrQuery[k]),
                        JIVariant.OPTIONAL_PARAM(),
                        new JIVariant(new Integer(RETURN_IMMEDIATE + FORWARD_ONLY))
                };

                JIVariant[] servicesSet = msvmServices.callMethodA("ExecQuery", params);
                iterateEnum(servicesSet);

            }       
    }

    private  static void iterateEnum(JIVariant[] servicesSet) {
        try {
            IJIDispatch wbemObjectSet = (IJIDispatch) narrowObject(servicesSet[0].getObjectAsComObject());
            JIVariant newEnumvariant = wbemObjectSet.get("_NewEnum");
            IJIComObject enumComObject = newEnumvariant.getObjectAsComObject();
            IJIEnumVariant enumVariant = (IJIEnumVariant) narrowObject(enumComObject.queryInterface(IJIEnumVariant.IID));
            List<Object[]> respArr = getEnumIterations(enumVariant);

            for (Object[] elements : respArr) {
                JIArray aJIArray = (JIArray) elements[0];
                JIVariant[] array = (JIVariant[]) aJIArray.getArrayInstance();

                for (JIVariant variant : array) {
                    IJIDispatch wbemObjectDispatch = (IJIDispatch) narrowObject(variant.getObjectAsComObject());

                    JIVariant[] v = wbemObjectDispatch.callMethodA("GetObjectText_", new Object[] {});
                    System.out.println("----------------------------------------------------------------------");
                    System.out.println(v[0].getObjectAsString().getString());
                    System.out.println("----------------------------------------------------------------------");
                }
            }
        } catch (JIRuntimeException e) {
            e.printStackTrace();
        } catch (JIException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {    
        msvmServices = createCOMServer("root\\virtualization");
        getVMList();
    }

}

private static List<Object[]> getEnumIterations(IJIEnumVariant enumVariant) {
    List<Object[]> list = new ArrayList<Object[]>(); 
    int i=0;

    for (i=0;i<100;i++) {
        try {
            list.add(enumVariant.next(1));
        }catch (JIRuntimeException jre) {
            break;
        }
        catch (JIException jie) {
            break;              
        }
        catch (Exception e) {
            break;
        }
    }
    return list;
}

另外,提供管理员用户名和密码。
它应该可以工作。
谢谢。

You can use JInterop for managing all operations of HyperV.
Download JInterop from here.
Add all JInterop jar files to your project build path.
Following is an example of getting all VMs of Hyper server:

public class ManageHyperV {
    static final int RETURN_IMMEDIATE = 0x10;
    static final int FORWARD_ONLY = 0x20;     
    private static final int STOP = 0;
    private static final int START = 1;
    static IJIDispatch msvmServices = null;


    private static IJIDispatch createCOMServer(String namespace) { //root//virtualization
        JIComServer comServer;
        try {           
            JISystem.getLogger().setLevel(Level.WARNING);
            JISystem.setAutoRegisteration(true);

            JISession session = JISession.createSession(domainName,userName,password);
            session.useSessionSecurity(false);
            comServer = new JIComServer(valueOf("WbemScripting.SWbemLocator"),hostIP,session);

            IJIDispatch wbemLocator = (IJIDispatch) narrowObject(comServer.createInstance().queryInterface(IID));
            //parameters to connect to WbemScripting.SWbemLocator
            Object[] params = new Object[] {
                    new JIString(hostIP),//strServer
                    new JIString(namespace),//strNamespace
                    //                  new JIString("ROOT\\CIMV2"),
                    JIVariant.OPTIONAL_PARAM(),//strUser 
                    JIVariant.OPTIONAL_PARAM(),//strPassword 
                    JIVariant.OPTIONAL_PARAM(),//strLocale 
                    JIVariant.OPTIONAL_PARAM(),//strAuthority
                    new Integer(0),//iSecurityFlags 
                    JIVariant.OPTIONAL_PARAM()//objwbemNamedValueSet
            };
            JIVariant results[] = wbemLocator.callMethodA("ConnectServer", params);
            IJIDispatch wbemServices = (IJIDispatch) narrowObject(results[0].getObjectAsComObject());
            return wbemServices;
        } catch (JIException jie) {
            System.out.println(jie.getMessage());
            jie.printStackTrace();
        } catch (JIRuntimeException jire) {
            jire.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static void getVMList() throws JIException {

            String temp = "select * from Msvm_ComputerSystem";          
            String[] arrQuery = new String[]{temp};

            for (int k=0;k<arrQuery.length;k++) {

                Object[] params = new Object[] {
                        new JIString(arrQuery[k]),
                        JIVariant.OPTIONAL_PARAM(),
                        new JIVariant(new Integer(RETURN_IMMEDIATE + FORWARD_ONLY))
                };

                JIVariant[] servicesSet = msvmServices.callMethodA("ExecQuery", params);
                iterateEnum(servicesSet);

            }       
    }

    private  static void iterateEnum(JIVariant[] servicesSet) {
        try {
            IJIDispatch wbemObjectSet = (IJIDispatch) narrowObject(servicesSet[0].getObjectAsComObject());
            JIVariant newEnumvariant = wbemObjectSet.get("_NewEnum");
            IJIComObject enumComObject = newEnumvariant.getObjectAsComObject();
            IJIEnumVariant enumVariant = (IJIEnumVariant) narrowObject(enumComObject.queryInterface(IJIEnumVariant.IID));
            List<Object[]> respArr = getEnumIterations(enumVariant);

            for (Object[] elements : respArr) {
                JIArray aJIArray = (JIArray) elements[0];
                JIVariant[] array = (JIVariant[]) aJIArray.getArrayInstance();

                for (JIVariant variant : array) {
                    IJIDispatch wbemObjectDispatch = (IJIDispatch) narrowObject(variant.getObjectAsComObject());

                    JIVariant[] v = wbemObjectDispatch.callMethodA("GetObjectText_", new Object[] {});
                    System.out.println("----------------------------------------------------------------------");
                    System.out.println(v[0].getObjectAsString().getString());
                    System.out.println("----------------------------------------------------------------------");
                }
            }
        } catch (JIRuntimeException e) {
            e.printStackTrace();
        } catch (JIException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {    
        msvmServices = createCOMServer("root\\virtualization");
        getVMList();
    }

}

private static List<Object[]> getEnumIterations(IJIEnumVariant enumVariant) {
    List<Object[]> list = new ArrayList<Object[]>(); 
    int i=0;

    for (i=0;i<100;i++) {
        try {
            list.add(enumVariant.next(1));
        }catch (JIRuntimeException jre) {
            break;
        }
        catch (JIException jie) {
            break;              
        }
        catch (Exception e) {
            break;
        }
    }
    return list;
}

Also, provide administrator username and password.
It should work.
Thanks.

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