How to edit and save uac protected files especially the hosts file (DNS mapping) through bat file / java.
Java throws IO exception. Because the file is not writable. Set write also fails.
File file = new File("C:\\Windows\\System32\\drivers\\etc\\hosts");
file.setWritable(true);
FileUtils.writeStringToFile(file, "127.0.0.1 test.com", true);
Exception
File 'C:\Windows\System32\drivers\etc\hosts' cannot be written to
You have to give write permission default host file has read and execute permission only
Related
I have a shared drive (NAS) attached to my Linux server wherein I am able to create and write to file usiing the following Java code.
String filePath = remotePath + fileName;
BufferedWriter fileWriter = new BufferedWriter(new FileWriter(filePath));
fileWriter.write(fileContents);
fileWriter.close();
File file = new File(filePath);
file.setExecutable(true);
file.setWritable(true);
file.setReadable(true);
I have tried to log the permission attribute too using canExecute(), canWrite(), canRead() and all the output are logged as true.
But this newly created file is not inheriting the folder permissions. When user try to access(Read/Delete) files using Linux script it gives permission denied.
The user running the script is the folder owner while the file shows owner as root. Due to policy, the user doesn't have sudo rights. How can I make it accessible?
If i understand this right, then your Java process is running as root. The created file is owned by the user that runs the process. Which is in your case root.
I see two options for you:
Let the Java process that creates the file run as the user that owns the directory. So the files will be owned and accessible by the user.
If the Java process must run as root then you need to change the owner of the file after it had been written. see Change file owner group under Linux with java.nio.Files on how this can be done.
I have a Java application were the user can create a text file and save it wherever he wants on his computer using this code :
File txtFile = new File( path );
Writer writer = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( txtFile ), "UTF-8" ) ); // Error occurs here.
But many users using Windows 7 reported that when saving the file to "C:\", they get "Access is denied" error. I found that this is because they need administrator permissions to save the file in such path in Win7.
Instead of showing a warning message to the user: " You can't save the file at this path ", can i save the file in this path somehow, like if there is a way to have Administrator permissions in Win7 through Java code, or something like that ?
Short answer - no.
If you need to save to C drive, they need permissions. If this program just needs to create files, you can use the users temp folder. See System.getProperty()
Windows Vista and Windows 7 have UAC enabled. UAC denies creating new files in SOME locations, without administrative privileges.
Check your permissions and make sure to execute the java executable in ADMINISTRATIVE Account, OR disable UAC.
To do that, go to "Start" type in "CMD.EXE" -> right click on the cmd.exe file and Run As Administrator. Then navigate to the location containing the .class file. Then type in java ClassFile and hit enter
Are you using cmd , i.e. Dos to run your file or eclipse? Whatever you are using
It looks like you are running as default user.
in windows 7, UAC by default blocls writimg to system.directory.
Do the following and hopefully it should work1
> If cmd.exe
> Then when you open run from start menu. Right click it, select run as administrator and then run your application
>
> If eclipse/any other IDE
>
> Close existing, right clicl eclipse,select run as admim and then run your application
>
> Hope this helps
I have directory where I will look for ZIP files and then I will extract files from those ZIP files with TrueZIP 7. My problem is that when I have my application running, I cannot delete/edit file while application is running in Windows. I don't understand why this is happening, because when I'm accessing file in Windows my application is not doing anything with files, so why these files are locked to my Java app?
I have following code:
Path dir = FileSystems.getDefault().getPath(directory);
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path file : stream) {
// do something with file
}
} catch (IOException | DirectoryIteratorException x) {
System.err.println(x);
}
// Why those files are still locked to my app even when execution is here ???
I forgot to call unmount :
TVFS.umount();
Now I can delete/edit files.
Why does this code cause an error: access denied?
public void armazenaPerfil() throws FileNotFoundException, IOException {
FileOutputStream out = new FileOutputStream(this.login + "_perfil.mbk");
ObjectOutputStream objOut = new ObjectOutputStream(out);
objOut.writeObject(this);
System.out.println("Escrevi!");
objOut.close();
}
The error message:
ric93_perfil.mbk(acess denied)
java.io.FileNotFoundException
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:212)
at java.io.FileOutputStream.<init>(FileOutputStream.java:104)
at br.uefs.ecomp.myBook.model.Perfil.armazenaPerfil(Unknown Source)
Access denied problems are basically the operating system saying "You are not allowed to write that". Basically, an OS-level access control / permissions issue is preventing you from reading or writing the file at the specified location.
When you write a file using a relative pathname, the JVM will attempt to write it in a location relative to the running application's current working directory. What directory that will be depends on how the JVM is launched, but if you launch from a command prompt using the java command, it will be the command shell's current directory.
You can find out what the current director actually is using the one-liner suggested by Brendan Long:
System.out.println(new File(pathname).getAbsolutePath());
where pathname is the pathname of the file you were trying to read or write. Note that this doesn't actually check that the pathname refers to an existing file, or tell you that you should be able to create or open the file. It merely tells you what the absolute pathname for the file would be.
i'm having the the post's title error when trying to write a file on a window folder , mounted on unix system. I've developed a web service which runs inside a Tomcat 6 on a linux os and need to write on a windows network folder. System administrators have mounted it on the Linux sever and have no problem to create and modify a file on it.
When i try to execute the posted code i get the following exception :
Permission denied
java.io.IOException: Permission denied
at java.io.UnixFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:850)
The weird thing is that it seems to be related to the File.createNewFile method on a network folder , in fact the service can write on local file system without problems, both on debug (the pc i use to develop the service) and a tomcat folder system administrators have provided me on the linux server. The file gets created but is empty and the log entry following the create method doesn't get printed. Moreover if i use a plain outputstream to create and write the file i've no problems.
I cannot find any explanation about the exception on the web. Since i'm not very experienced with java , i'd like to understand why i'm getting this error. Am i using it in the wrong way ? Is it a bug of the library ? Do i miss to pass some parameter ?
As stated , i've solved the problem using a plain outputstream, this is a question to improve my understanding of java.
FileOutputStream fos = null;
try{
log.info(String.format("file length: %s",streamAttach.length));
log.info(String.format("check File : %s",filename));
File f = new File(filename);
if(f.exists())
...
boolean done= f.createNewFile();//here comes the exception
//nothing of the following happens
if(!done)
throw new NWSException("error creating file");
log.info(String.format("file %s creato", nomeFile));
thank you in advance for any answer
I ran into this problem recently and found that java.io.File.createNewFile() actually requires the "Change Permissions" permission (you can find this entry under Security->Advanced when checking folder permissions). Without this it will create the file and then subsequently throw an IOException.
It's deceptive because you will still be able to create files on the folder when manually testing, however createNewFile() will still fail if it doesn't have this particular permission (presumably such that it can change the permissions on the file its creating).
If you are using Netapp that shares an NTFS (CIFS) style filesystem to Unix you could be experience "NFS is not allowed to change permissions on a file in an NTFS-style security volume." (TR-3490 page 16)
Options here are to change to a unix filesystem or set the cifs.ntfs_ignore_unix_security_ops flag to on for the file system which quiches the NFS permission error.
java.io.UnixFileSystem.createFileExclusively(Native Method) opens the file with the O_EXCL and 0666 umask so I would get a EACCES, which really was a NFS3RR_ACCES
open("/net/storage01-a/filer/myfile", O_RDWR|O_CREAT|O_EXCL, 0666) Err#13 EACCES
Also you can use OutputStream to create the file, that does not use O_EXCL it seemes
It definitely not Java specific problem. If this Unix folder is mapped to your windows try to open file explorer and create file in this directory. I believe that you will get permission denied too. In this case fix this problem or ask your system administrator to help you.
Good luck!