Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permission Denied When Writing File to Default Temp Directory

My program does some fairly intensive operations, so I use a scratch file in order to speed things up. I use the following Java code:

File scratchFile = new File(System.getProperty("java.io.tmpdir") + "WCCTempFile.tmp");
if (!scratchFile.exists())
    scratchFile.createNewFile();

This code works just fine on Mac OS X and Windows. It creates a scratch file in the Java temporary directory, which is determined by the operating system.

However, when I try this program on Linux (specifically Linux Mint), I get the following error on the line "scratchFile.createNewFile()"

java.io.IOException: Permission Denied

I'm really confused by this error because I figured that the temp directory that is gathered by the System.getProperty("java.io.tempdir") method would be one that the user could write to (and it is on other operating systems). Is this not the case on Linux? Is there some way to grant access to the temp directory? Is there some other directory I'm supposed to be using?

like image 851
Thunderforge Avatar asked Feb 13 '13 01:02

Thunderforge


People also ask

How do I fix permissions denied?

Right-click the file or folder, and then click Properties. Click the Security tab. Under Group or user names, click your name to see the permissions that you have. Click Edit, click your name, select the check boxes for the permissions that you must have, and then click OK.

How do I fix unable to create a temporary file?

Creating an executable file requires creation of temporary files. This error has the following cause and solution: The drive that contains the directory specified by the TEMP environment variable is full. Delete files from the full drive or specify a different drive in the TEMP environment variable.

What does Permission 644 and 755 mean for a file?

755 - owner can read/write/execute, group/others can read/execute. 644 - owner can read/write, group/others can read only.

How do I fix permission denied to a directory in Linux?

For solving this error, you need to add the correct permissions to the file to execute. However, you need to be a “root” user or have sudo access for changing the permission. For changing the permission, Linux offers a chmod command.


1 Answers

On Linux java.io.tmpdir is commonly set to /tmp (note the missing trailing /). Instead of messing around with extra embedded slashes, it's a lot cleaner to use the two-parameter File constructor

File scratchFile = new File(System.getProperty("java.io.tmpdir"),"WCCTempFile.tmp");

That way you don't have to worry about trailing slashes or not.

like image 154
fvu Avatar answered Sep 23 '22 18:09

fvu