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]
1
  • 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) Commented Nov 7, 2012 at 5:57

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

"the IP address is automatically assigned by DHCP". Not necessarily.
I mean static IP address. I could do the same in linux too, I just want to know if there is a platform independent way or not?
@Thilo : Ya ofcourse its not necessary .if you use DHCP hen it is assigned automatically
@ehsun7b well i don't think there is a platform independent way but i'll edit the post to give you furthur info

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.