Getting An "Access Denied" Error While Editing "Hosts" File Through Java Program. Program needs an ADMINISTRATIVE PRIVILEGE To Edit The Hosts File.
So Is there any Solution To provide That Administrative privilege to java program so that It can edit Hosts File.
** Note ** I am Using Window 8.1 OS. Hosts File Location is: C:\Windows\System32\Drivers\etc\Hosts
Have a look at the AccessController API: Java Access Controller
one thing is for sure though, when the user runs the Java program, the user must right-click and select 'Run As Administrator'.
Another option is that you could read in the file, make your changes in memory, (file could be read in as a string), and then write out the file to replace the HOSTS file. Which again, you definitely need Administrator privileges.
Quick Google search also showed source code for a package that let's you edit hosts file, you might want to take a look at that. It still definitely needs the administrator permissions though. Link here
Have a look at this previous SO question, it might give you a few ideas.
The main issue with writing this kind of programs is the permissions. The Hosts file is a critical part of every operating system, and it's only logically to have layers of security around it to prevent any random program from changing the hosts file, hence all these extra hurdles.