Folder rootFolder = si.getRootFolder();
HostSystem host = null;
host = (HostSystem) new InventoryNavigator(
rootFolder).searchManagedEntity("HostSystem", hostname);
HostNetworkSystem hns = host.getHostNetworkSystem();
// add a virtual switch
HostVirtualSwitchSpec spec = new HostVirtualSwitchSpec();
spec.setNumPorts(8);
hns.addVirtualSwitch(switchName, spec);
// add a port group
HostPortGroupSpec hpgs = new HostPortGroupSpec();
hpgs.setName(portGroupName);
hpgs.setVlanId(0); // not associated with a VLAN
hpgs.setVswitchName(switchName);
hpgs.setPolicy(new HostNetworkPolicy());
hns.addPortGroup(hpgs);
// add a virtual NIC to VMKernel
HostVirtualNicSpec hvns = new HostVirtualNicSpec();
hvns.setMac("00:50:56:7d:5e:0b");
HostIpConfig hic = new HostIpConfig();
hic.setDhcp(false);
hic.setIpAddress("10.20.143.204");
hic.setSubnetMask("255.255.252.0");
hvns.setIp(hic);
String result = hns.addVirtualNic("VMKernel", hvns);
System.out.println(result);
System.out.println("Successful created : " + switchName);
}