tags:

views:

532

answers:

5

I want to improve the cross platform behavior of a java application. However, its test suite currently assumes the existence of the /tmp directory.

What is the best equivalent location on the windows platform? N.B. I most definitely do not want to assume the user has admin rights but I do want it to work on at least XP, Vista & Windows7.

Is there an existing environment variable that would help, and/or a set of preferred locations I could try in order of preference?

+22  A: 

The system property java.io.tmpdir can be used for the user's temp directory:

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

This may be preferred over File.createTempFile (which, in any case, uses the tmpdir system property under the hood) in the instance where you want to search for temp files (for example, cached data from a previous invocation of your application, which sounds like it might be the case from your question).

You can change the value of the system property by providing a runtime override on the command line (a JVM argument): -Djava.io.tmpdir=C:\foo\bar

Note: the "trailing slash" issue descibed in the comments to seth's answer below can be avoided by using the relevant File constructor:

String fileName = "foobar.txt"
String tmpPath = System.getProperty("java.io.tmpdir");
File tmpFile;
tmpFile = new File(tmpPath + File.separator + fileName); //possible problem
tmpFile = new File(new File(tmpPath), fileName); //OK!

Obviously windows also has an DOS environment variable %TEMP% which could be used from any scripts which you have

oxbow_lakes
Yep - I always use that and it generally works
DroidIn.net
I use this too.
pjp
+8  A: 

Why not use java.io.File.createTempFile()?

seth
He might be looking for files which have been dropped in the user's temp directory, as opposed to creating them
oxbow_lakes
I love when people hard code paths and get mad at Microsoft (and other companies) when things change... Framework calls/SDKs are your friends
Matthew Whited
@oxbow_lakes - I've had problems with java.io.tmpdir http://rationalpi.wordpress.com/2007/01/26/javaiotmpdir-inconsitency/ Maybe it's changed though as it's been awhile since I used Java
seth
The `createTempFile` methods just uses the `java.io.tmpdir` property under the hood! See `File` lines 1668-1676
oxbow_lakes
@oxbow_lakes - Yes, I know that but I've had problems before with the trailing slash on different OS (like in the link I pasted) as well as accidentally over-writing files. I just found it safer to use createTempFile instead of using my own homegrown system.
seth
Then use the `File` constructor taking the parent directory and file name and the problem disappears
oxbow_lakes
I've modified my answer to take this into account
oxbow_lakes
-1 Because the question is about the "temporal directory" and not how to create a "temporal file". I would +1 if a could for the article cited though.
OscarRyz
A: 

Use File.createTempFile("myPrefix","mySuffix").

Paul Brinkley
+2  A: 

Windows defines environment variables TEMP and TMP both giving the current user's temporary folder.

Richard
You might find that TMP exists only on machines which have developer tools installed.
ChrisW
A: 

If you want to insist on doing something that is windows specific you can use the %temp% environment variable.

shoosh