Problems with FileOutputStream - java

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.

Related

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

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.

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

Directory not empty java.io.IOException

I need to launch an external app called TeamViewer from my JavaFX application.
So I've TeamViewer.app file which I'm copying to a temporary folder and launching it using:
Runtime.getRuntime().exec("open /path/to/Teamviewer.app");
But this is throwing Directory not empty IOException.
I also tried launching using shell file where I wrote "open /path/to/Teamviewer.app" command to launch.sh and launched launch.sh using process created by ProcessBuilder. if I run launch.sh from terminal, it works. But from java program,
following exception is thrown:
SEVERE: null
java.io.IOException: Cannot run program "sh" (in directory "/Applications/ColorDx.app/Contents/Java"): error=66, Directory not empty
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047)
at com.sc.colordx.controller.ColorDxSupportController.executeCommand(ColorDxSupportController.java:288)
at com.sc.colordx.controller.ColorDxSupportController.launchSetup(ColorDxSupportController.java:126)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
What could be the reason for directory not empty exception? It has to be non empty as I've copied TeamViewer.app there. Could this be a multi threading issue? Means I'm copying TeamViewer.app first and immediately launching it. Is there a chance that launch command is called before copying is finished?
TIA
As I'd suspected, there was a problem with runtime itself! There's one more JavaFX app that acts as installer for this app (app with Runtime problems). For installation of this app I had used following code to copy contents of .app file:
public static final void copyFile( File source, File destination ) throws IOException {
FileChannel sourceChannel = new FileInputStream( source ).getChannel();
FileChannel targetChannel = new FileOutputStream( destination ).getChannel();
sourceChannel.transferTo(0, sourceChannel.size(), targetChannel);
sourceChannel.close();
targetChannel.close();
}
Above code didn't preserve file attributes while copying. Replacing above method with Files.copy using COPY_ATTRIBUTES option solved the problem.

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."

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