0

Is it possible to change to IP address of the machine using Java?

Something equivalent to this linux command:

ifconfig eth0 [new-ip-address]
ehsun7b
  • 4,796
  • 14
  • 59
  • 98
  • 1
    You'll probably need you java program to run as root. Then you could run the above command, or do operating system specific tricks (like writing JNI doing the appropriate syscalls) – Basile Starynkevitch Nov 07 '12 at 05:57

1 Answers1

1

well java.net.NetworkInterface provides the information about your network interface. But, the IP address of your machine is highly OS specific. I don't think there is an easy way to change it though.

Furthur, the IP address is automatically assigned by DHCP.(I you use DHCP as pointed out by Thilo)

however I have successfully implemented a code which could run some of the DOS commands using java.lang.Runtime class.

So, you can try to assign static IP address using netsh int ip set address "local area connection" static 192.168.0.101 255.255.255.0 192.168.0.254 1 type of DOS command in java.

I don't think there is a platform independent way but you can find the OS information using these links

1.Using Java to get os level system Information

2.Finding Operating System Information

3.How to get System Information using Java

you can use some if-else statements based on the system info then call the respective commands

Community
  • 1
  • 1
Bhavik Shah
  • 5,125
  • 3
  • 23
  • 40