using java to edit host file

Questions about programming languages and debugging
Post Reply
User avatar
Kirk
suck-o enforcer
suck-o enforcer
Posts: 547
Joined: 25 Apr 2009, 16:00
14
Contact:

using java to edit host file

Post by Kirk »

So I want to use java to write a program that will edit the host file in windows to add some info to it. When I get home I plan on googling this but would like your feedback also. I'm looking just for a few general suggestion on how to start it or what to put in it. Please don't write it for me, just help.

This should be super freaking easy. I will need to open notepad with elevated privileges in order to edit the host file.

Thanks guys

Kirk

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Re: using java to edit host file

Post by bad_brain »

have a look at this one, it will be definitely needed (imo) because of the security restrictions of Java AND of the OS:
http://download.java.net/jdk7/archive/b ... oller.html" onclick="window.open(this.href);return false;
Image

User avatar
Kirk
suck-o enforcer
suck-o enforcer
Posts: 547
Joined: 25 Apr 2009, 16:00
14
Contact:

Re: using java to edit host file

Post by Kirk »

I'm using eclipse to code it and compile it. I'm stuck good. Would I even have to use a text editor like notepad to make the changes or could I change the file directly some how?

User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Re: using java to edit host file

Post by maboroshi »

You can open any file in Java and Edit it with the changes you want with in your code, without any additional editor like notepad or needed input by a user. Look at File Handling/File IO and the modes associated to those. These are the modes that you would open the file in read, write, read + write, append etc

The problem may be in handling the elevated permissions. However I believe you may be able to bypass this simply by writing a registry value on execution of your script (In a similar fashion to above), or using a launch batch script as an example. You will have to look for that registry value through internet searching.

*cheers

User avatar
Kirk
suck-o enforcer
suck-o enforcer
Posts: 547
Joined: 25 Apr 2009, 16:00
14
Contact:

Re: using java to edit host file

Post by Kirk »

Couldn't I just have it prompt for the user to ok it some how?

User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Re: using java to edit host file

Post by maboroshi »

The UAC is a Windows OS kernel security feature. You have to create a manifest file with the required user privilege level: http://msdn.microsoft.com/en-us/library/bb756929.aspx

User avatar
Kirk
suck-o enforcer
suck-o enforcer
Posts: 547
Joined: 25 Apr 2009, 16:00
14
Contact:

Re: using java to edit host file

Post by Kirk »

I see this is going to be a little more difficult than originally thought. This is a lot of stuff just to append some lines in a file. Good little learning project though.

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: using java to edit host file

Post by ayu »

Start by writing the program simply because it's fun :) (like keep a host file in a user space location for now).
Then you should be able to use eclipse to create the manifest file rather easily, just like you can with Visual Studio these days.

Have a look at this

Code: Select all

http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-35.htm
"The best place to hide a tree, is in a forest"

Post Reply