4

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?

2
  • 1
    Related: stackoverflow.com/questions/11647629/… Commented Nov 4, 2020 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. Commented Nov 4, 2020 at 22:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.