Permission issue to file written to shared drive (NAS) using Java filewriter - java

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.

Related

Edit UAC Protected file in windows

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

Exception in thread "main" java.nio.file.AccessDeniedException when creating a file [duplicate]

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

what is the default Storage location in File Object

In this code i didn't mention the path for the file hello.xls. But, I am reading the values from hello.txt file but i don't know where it gets stored. Is it stored in the JVM memory or some where else. if so what is the maximum size. I am using unix box.
sample java code:
File f = new File(hello.xls);
InputStream f = new FileInputStream(f);
If it is store some where in the server, Please suggest, how to handle without storing the files in the server to read the values and write the values in the same excel sheet.
Default storage location in File object is a directory obtained by executing the line :
System.getProperty("user.dir"); //represents the current directory the user is executing the program, rather than where the program is located.
It's the directory from where the java was run -- where you started the JVM.
According to the javadocs, if you don't specify a path in the file constructor, the file is assumed to be in the directory pointed to by the
"system property user.dir, and is typically the directory in which the
Java virtual machine was invoked."

Change permission of a created file so only the current user can read it

I need to set the permission to read a file to a specific user of the operating system, how can I do this in Java?
Edit:
The file will be created with permissions just to the user running the application, than it needs to set read permission to a single user, other users will not have the permissions to read the file.
Use the methods setExecutable, setReadable, and setWritable in java.io.File. You can use these to change any permission bit of a file you own. Direct link: http://download.oracle.com/javase/6/docs/api/java/io/File.html#setReadable%28boolean%29.
Testing this on MacOSX revels that only the user read value is changed. When program
import java.io.File;
public class Test
{
public static void main(String[] args)
{
File f = new File("test.txt");
f.setReadable(true);
}
}
the folloiwng happens.
$ touch test.txt
$ chmod 000 test.txt
$ javac Test.java
$ java Test
$ ls -l test.txt
-r-------- 1 morrison staff 0 Jun 7 13:28 test.txt
If you're targetting Windows Vista/7, build your JAR as EXE and embed a manifest requesting for Admin rights.
If it's just an I/O problem, use the default File methods setReadable, setWritable, setExecutable :)
In regards to making the permissions for a single user, start with using ncmathsadist's code to add read permissions, then change the owner of the file to whoever needs access.
I found in the Ant source code they use for the change-owner task. For unix this can be found in the Ant source tree at org/apache/tools/ant/taskdefs/optional/unix/Chown.java. You might be able to include this and use it as an API call to change the user programmatically.
if you want to change file permissions on old Java versions like Java 5, you can use this:
Runtime.getRuntime().exec("chmod 000 " + PATH + fileName);
on windows you'll have to replace chmod with the appropriate CACLS.exe command syntax

java.io.IOException: Permission denied on network folder

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!

Categories

Resources