Environment Variable to Control Java.Io.Tmpdir

Environment variable to control java.io.tmpdir?

Hmmm -- since this is handled by the JVM, I delved into the OpenJDK VM source code a little bit, thinking that maybe what's done by OpenJDK mimics what's done by Java 6 and prior. It isn't reassuring that there's a way to do this other than on Windows.

On Windows, OpenJDK's get_temp_directory() function makes a Win32 API call to GetTempPath(); this is how on Windows, Java reflects the value of the TMP environment variable.

On Linux and Solaris, the same get_temp_directory() functions return a static value of /tmp/.

I don't know if the actual JDK6 follows these exact conventions, but by the behavior on each of the listed platforms, it seems like they do.

When does System.getProperty(java.io.tmpdir) return c:\temp

In MS Windows the temporary directory is set by the environment variable TEMP. In XP, the temporary directory was set per-user as Local Settings\Temp.

If you change your TEMP environment variable to C:\temp, then you get the same when you run :

System.out.println(System.getProperty("java.io.tmpdir"));

Java Web Start - Access environment variable java.io.tmpdir in jnlp

A little late, but I still want to post my solution:

I worte a custom Launcher class and configured it in my JNLP file (< application-desc main-class="de.my.package.launcher.Launcher">).
In my Launcher class, I set the desired variables and afterwards I call the main method of the Equinox Launcher.
I know that´s not a very charming solution, but at least it works ;-)

How to set the location of junit TemporaryFolder to an environment variable

Based on our long-lasting discussion I now understand that you ask this:

How to configure JUnit so it uses a specific folder for creating the temporary files.

(If I get your question properly, please edit it accordingly and change the title.)

JUnit 4.11+

The easiest way is to use the new constructor TemporaryFolder(File parentFolder). This is the preferred way.

JUnit < 4.11

You are aware that the org.junit.rules.TemporaryFolder uses the the system property java.io.tmpdir. Actually, when you look at the source code, it uses internally the File.createTempFile(prefix, suffix) method which uses this system property.

You will find exhaustive information here: Environment variable to control java.io.tmpdir?.

More generally about system properties and environment variables: Java system properties and environment variables

However, you should notice this sentence from the Java-doc:

A different value may be given to this system property when the Java
virtual machine is invoked, but programmatic changes to this property
are not guaranteed to have any effect upon the temporary directory
used by this method.

If you want to be sure you have the temporary folder always under your control, you may create your own version of org.junit.rules.TemporaryFolder - it's not so difficult, you just use the File.createTempFile(prefix, suffix, directory) with explicitly assigned the 3rd parameter directory.



Related Topics



Leave a reply



Submit