In this video I have shown how you can change IP address using java project.
I have shown this using Eclipse IDE.
1. Project Structure in Eclipse IDE is shown below. Create a Java Project.
2. The first java class is my_main.java. It will internally execute the commands as shown in the video.
Please set your adapter name, ip address, subnet mask, default gateway, dns 1 and dns 2 in the code.
my_main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.jinujawad.com; import java.io.PrintWriter; public class my_main { public static void main(String[] args) { String adapter_name="Wi-fi"; String ip_address="192.168.100.55"; String subnet_mask="255.255.255.0"; String default_gateway="192.168.100.1"; String dns_1="8.8.8.8"; String dns_2="8.8.4.4"; String[] command = { "cmd", }; Process p; try { p = Runtime.getRuntime().exec(command); new Thread(new SyncPipe(p.getErrorStream(), System.err)).start(); new Thread(new SyncPipe(p.getInputStream(), System.out)).start(); PrintWriter stdin = new PrintWriter(p.getOutputStream()); stdin.println("netsh int ip set address "+adapter_name+" static "+ip_address+" "+subnet_mask+" "+default_gateway); stdin.println("netsh int ip set dns "+adapter_name+" static "+dns_1+" primary"); stdin.println("netsh interface ip add dns "+adapter_name+" "+dns_2+" INDEX=2"); stdin.close(); p.waitFor(); } catch (Exception e) { e.printStackTrace(); } } } |
3. my_main.java will call SyncPipe.java internally to show the output in console.
SyncPipe.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.jinujawad.com; import java.io.InputStream; import java.io.OutputStream; class SyncPipe implements Runnable { public SyncPipe(InputStream istrm, OutputStream ostrm) { istrm_ = istrm; ostrm_ = ostrm; } public void run() { try { final byte[] buffer = new byte[1024]; for (int length = 0; (length = istrm_.read(buffer)) != -1; ) { ostrm_.write(buffer, 0, length); } } catch (Exception e) { e.printStackTrace(); } } private final OutputStream ostrm_; private final InputStream istrm_; } |
4. After executing the mymain.java in Administrator mode. The IP address would be changed as below shown.