How to access Java tmp directory

Java defines an environment variable with the user temp directory that can be accessed programatically.

Syntax:

String tmpDirectory = System.getProperty("java.io.tmpdir");

Here is an example that accesses the java.io.tmpdir variable from from the System object. The variable is saved in a String and displayed in the output.

Create the following java file:

public class TmpDirectory {

	public static void main(String[] argv){
		String tmpDirectory = System.getProperty("java.io.tmpdir");

		System.out.println( "The tmp directory is " + tmpDirectory ); 
	}
	
}

The output will be:

The tmp directory is C:\Users\Olivier\AppData\Local\Temp\

The variable can be passed to the JVM using the parameter java.io.tmpdir, example -Djava.io.tmpdir=/tmp. In a J2EE environment the temporary directory points to the application server tmp directory.


References:

Setting up the Java IO Temp Location

Recent Comments