Runtime修改本地IP

public class IPBean {
	private String newIP;
	private String mask;
	private String gateway;

	public void setNewIP(String newIP) {
		this.newIP = newIP;
	}

	public void setMask(String mask) {
		this.mask = mask;
	}

	public void setGateway(String gateway) {
		this.gateway = gateway;
	}

	public IPBean() {
	}

	public IPBean(String newIP, String mask, String gateway) {
		this.newIP = newIP;
		this.mask = mask;
		this.gateway = gateway;
	}

	public void execute() throws Exception {
		//利用进程来修改IP
		Runtime.getRuntime().exec(
				"netsh interface ip set addr \"本地连接\" static " + newIP + " "
						+ mask + " " + gateway + " 1");
	}
}

public class IPUtil {
	public static void main(String[] args) {
		IPBean bean = new IPBean();
		
		//设置内网IP
		bean.setNewIP("192.168.1.2");
		bean.setMask("255.255.255.0");
		bean.setGateway("192.168.1.1");
		
		try {
			bean.execute();
			System.out.println("Success");
		} catch (Exception ex) {
			System.err.println(ex.toString());
		}
	}
}

你可能感兴趣的:(bean)