2

How can I have Java programmatically set the IP address and hostname, rather than have to set it in my PC's host file?

I am writing a java program to test if a web application that resides behind a load-balancer is responding or not. Say the application is: app.company.com, and it resides on two different servers, with ip addresses: 1.1.1.1 and 2.2.2.2. Currently my test application updates the PC's hosts file, setting either "1.1.1.1 app.company.com" or "2.2.2.2 app.company.com" and then uses Apache HttpClient v4.5.10 to call the web application via: app.company.com And this works, with the change in the hosts file directing the call to the desired server.

But I would prefer to do this programmatically rather than updating the hosts file. Thoughts?

Eric Kemp
  • 21
  • 3
  • 1
    Related: https://stackoverflow.com/questions/11647629/how-to-configure-hostname-resolution-to-use-a-custom-dns-server-in-java – chrylis -cautiouslyoptimistic- Nov 04 '20 at 22:05
  • To add this to chrylis's comment, you really want to use the resolver for this. Hard coding these addresses and names would be a mistake. Use a config file to set the name (or maybe the command line) like "app.company.com" and then use the normal lookup method to find the IP address. There might be special reasons to not do this, but they are very rare. – markspace Nov 04 '20 at 22:26

0 Answers0