Abolfazl74 February 2016

access denied to access to /etc/hosts

i can't access to hosts file of windows with java (just write) :

java.io.FileNotFoundException: C:\Windows\system32\drivers\etc\hosts (Access is denied)
at java.io.FileOutputStream.open(Native Method)

when i want append my text to hosts file get above error ...

this is my code :

    BufferedWriter bw = null;
    try {
        // APPEND MODE SET HERE
        bw = new BufferedWriter(new FileWriter(file,true));
        bw.write(text);
        bw.newLine();
        bw.flush();
    } catch(FileNotFoundException e) {
        e.printStackTrace();

    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {                       try {
        // always close the file
        bw.close();
        } catch (IOException ex) {
            Logger.getLogger(FileWrite.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

} // end test()

this code work for simple files in other location of windows .. (just get error on c:\windows\ ...

and : i use microsoft manifest to set administration access [not work]

set full access to hosts file [not work]

please help ..

tnx

Answers


Jurz February 2016

Check if your java is really running as administrator (i.e. write to a new file in the same folder). There is obviously access problem and to solve it you need to understand who you are (user/service of your app) and what are permissions of resource (file) you are trying to access.

here are two ways how you can try and debug it: 1) You don't have permissions to write the file (can you read it?) 2) You don't have permissions to enter the folder (can you list files in it)

Post Status

Asked in February 2016
Viewed 3,340 times
Voted 9
Answered 1 times

Search




Leave an answer