在WINDOWS中,可以用netsh来改IP。改IP时可能要等一会儿的,有时 蛮慢的,不过这不是JAVA的关系,是WINDOWS改IP的API本身就慢,因为 要刷新一些网络参数嘛。public class Execute { private static void setIP(String newip) throws Exception { Runtime.getRuntime().exec("netsh interface ip set addr \"本地连接\" static "+newip+" 255.255.255.0 192.168.32.1 1"); }
public static void main(String[] args) throws Exception { System.out.println("Begin to set the local ip address\nPlease wait..."); System.out.println("\nAfter setting the ip, the program will auto exit..."); setIP("192.168.32.100"); System.out.println("Set ip successful!"); } }
蛮慢的,不过这不是JAVA的关系,是WINDOWS改IP的API本身就慢,因为
要刷新一些网络参数嘛。public class Execute
{
private static void setIP(String newip) throws Exception
{
Runtime.getRuntime().exec("netsh interface ip set addr \"本地连接\" static "+newip+" 255.255.255.0 192.168.32.1 1");
}
public static void main(String[] args) throws Exception
{
System.out.println("Begin to set the local ip address\nPlease wait...");
System.out.println("\nAfter setting the ip, the program will auto exit...");
setIP("192.168.32.100");
System.out.println("Set ip successful!");
}
}
其中netsh中的参数255.255.255.0是指子网掩码,192.168.32.1 指的是默认网关,最后一个参数1指的是网关跳数