Can you please tell what is significance of "java.io.tmp" directory?
I noticed this directory is created under webapps folder in my tomcat.
Thanks!
The name itself is a great clue. Its java.IO.TMP, which common sense would tell us is about temporary ios. Since the java.io package is about files then it probably adds up this is a path to store temporary files.
It is not directory, it is property points to a directory to be used as temp folder(directory happens to have same name as property is a tomcat decision). When you want to create temporary file, this property is used as the location for the file. Some example
File tempFile = File.createTempFile("pattern", ".suffix");
tempFile.deleteOnExit();
JVM by default copies the value from TMP/TEMP environment variable. The value can be set when JVM starts. Tomcat sets this for rest of the application to use.
Related : Environment variable to control java.io.tmpdir?
Related
I have read that files within a jar file are not supposed to be modified, and I think that explains why getting an outputStream is not as simple as getting an inputStream calling getClass().getResourceAsStream() to read a file. When using properties files values are going to be retrieved during execution, but what if those values are subject to change? It would be tedious to recompile the program.
In my case those values are IP address, username and a path to an external file. So I guess I could not move the external file even by accident because my properties file in the jar have a different path to that file. Is this the way is supposed to be or is there a more flexible way that I am not aware of?
Usually property files are located outside of jar somewhere on production system's classpath. Values are usually changed once in a while via provisioning scripts / manually and then only application restart is required, not a re-compile.
So, to sum it up, I'd suggest not having properties file inside the jar.
I try to load webapp project's settings from own config.properties file, but there's no success: the file not found. I can't use ServletContext method, because i've access the file from ordinary class.
file = Config.class.getClassLoader().getResourceAsStream("/config.properties");
- it's returns null.
I've tried to put the file to WEB-INF and resources folders, still doesn't work :(
Any ideas how to force this work?
Try putting files in WEB-INF/classes .
If you put your config in src, than after compile it will be in WEB-INF/classes.
My recommendation for configuration files that would solve your problem is to place them in a folder that depends on an environment variable.
If you application is called MyApp, then you can force the user to place that file in the folder that points the environment variable MY_APP. You could in fact make it mandatory to run the application with a start up check.
Usually you should specify default values for your configuration parameters if you want your application to run without the environment variable.
To get enviroment variables in Java you can use System.getenv();
I have developed a web application which will create an xml file based on the user Input. It has some more functionality. I configured my application to store the xml file(s) in the project root folder. When I run it through eclipse It stores in the specified location. But If I manually put the war file in apache tomcat and run the application those newly created xml files are going into bin directory since I used relative path. Now I dont want it to be created under bin directory. I want those files to be created somewhere local in the system. Is there any way to do it ? Or else what is the best way to deal with those xml files. I am using spring MVC.
You could use a variable in your code would contain the first part of the path (the real location where you want to write the file). And inject the value of that variable from a properties file, that way you wouldn't hardcode anything, and still would be able to provide only the relative path in your code.
I'm writing a program that needs a generic temp folder. I'm trying to find details about the Windows Temp folders. There are two paths that I know about -
In each user directory under AppData\Local\Temp\
This may change depending Windows version?
In the system folder under Temp\ (C:\Windows\Temp)
I'm wondering exactly what Windows does to each of these. If Windows deletes files from either location, when does it do so? How can/should I use these directories for my programming?
EDIT: I have a bigger problem actually - Because of a certain engine I'm running indirectly with my program, which uses files I'm creating in a temp directory, I need a temp directory that doesn't use whitespace characters in the path. Java's System.getProperty("java.io.tmpdir") on Windows gives me the temp that's in the user directory, which on XP is under "Documents and Settings..."
Not good. Any suggestions? This is why I'm wondering about the C:\Windows\Temp\ directory...
This will give you the path to the windows temp directory in Java.
File.createTempFile("temp-file", "tmp").getParent()
Not quite. There is a user and system folder, the default location of which varies according the windows version, system folder name, and indeed in older versions of windows was the same for both the user and system case. However, these defaults can be over-ridden (they are on the system I'm using now, where they aren't on the same drive as the system folder).
The locations are stored in system variables. Some frameworks (.NET, VB6 and no doubt others) give you convient ways to find the paths rather than having to look up the system variable (e.g. System.IO.Path.GetTempPath in .NET).
Windows does not clean up the temporary folder for you (which is why it's worth blasting out old files it every few months on your own machine), it's up to you to play nice. Create a file or files unlikely to step on the names any other software is using (they should take care to do the same, and so any name should do, but it's always good to assume the worse of other code on the system), and delete files when you're done (or on application exit at least).
In .NET System.IO.Path.GetTempFileName() will create a new file in the temp area and return the name of it to you, that is reasonably guaranteed not to conflict with others' so use that or similar methods if you can.
It sounds like you have two programs that need to share temp files and one definitely doesn't want spaces in the path name. Probably the easiest thing to do is:
set the TMP and TEMP variable to a common directory
launch each application (from this modified environment) - which should pick up the temp variable
So at the command prompt you could do this:
set TMP=c:\mytemp
set TEMP=c:\mytemp
java -cp x;y;z my.application.Entry
run other application (hopefully it also reads the environment for temp/tmp)
Hope that helps.
To answer part of your question - if you're using .NET, you can use the Path.GetTempPath() method of the System.IO namespace to get the location of the temporary directory.
// Get the path of the temporary directory
string tempDir = Path.GetTempPath();
// "Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file."
string tempFile = Path.GetTempFileName();
The %TEMP% environment variable that's defined on my PC (XP SP3) uses the DOS-style abcdef~1 directory names - hence, if you can pull that variable, you should end up with a path without spaces.
e.g. Start>Run>%TEMP% takes me to C:\DOCUME~1\<user>\LOCALS~1\Temp
However, if a 'super-user' fiddles around with that variable and points it somewhere else, it's possible that things will fall over. You could look at something like this to retrieve the 8-char-and-no-spaces path.
use this code
try { String s=File.createTempFile("temp-file", "tmp").getParent();
System.out.println(s);
} catch (IOException ex) {
Logger. getLogger(Result.class.getName()).log(Level.SEVERE, null, ex);
}
you can try this way
System.out.println(File.createTempFile("temp-file", "tmp").getParent());
String property = "java.io.tmpdir";
String tempDir = System.getProperty(property);
System.out.println("OS current temporary directory is " + tempDir);
I have a web application deployed in WebLogic. In one of my java file, I tried to read PleaseNote.txt as following:
File file = new File("PleaseNote.txt");
Now WebLogic is taking PleaseNote.txt from its domain directory.My question is:
Why it is domain directory? Why not the directory where my java file which has the above line of code is in?
Is there any configuration which I am not aware of , but did unknowingly, for WebLogic to look in its domain directory?
What are the implications / side effects of using above line of code in production?
Any WeLogic experts, please respond.
Thank you
Regards
Chaitanya
Reading a file using that way makes your application less portable and not very robust: if you deploy your application on another application server, you'll have to find out where to put that PleaseNote.txt file again or the code will break.
This breaks the WORA (Write Once, Run Anywhere) principle and I'd consider this as a bad
practice.
So, I'd rather put this file in the classpath and use ClassLoader#getResourceAsStream(String name) to read it.
It is domain directory because it's corresponds to value of user.dir system variable, the place where java reads/writes files if path not explicitly set.
Why domain directory corresponds to user.dir ? Because you start Weblogic server here.
Regards
Alexander Rozhkov
when using new File(..) java looks for the file in the directory from where java.exe is started. In case of an weblogic domain, this is ofcourse the domain directory. This is default java behaviour.
When you want to load a file that is in de same directory as the class-file you are loading from, use ClassLoad.getResourcesAsStream(). If you want to load a resource from the classpath use the same method, but prefix your file with "/".